打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
让企业微信机器人(webhook)帮你干一部分活

Webhook是一个API概念,webhoo是一种web回调或者http的push API.Webhook作为一个轻量的事件处理应用,正变得越来越有用。

一定要结合企业微信API文档:https://work.weixin.qq.com/api/doc/90000/90136/91770

核心:webhook唯一url+规定的参数

用途:公司内部自动推送新闻、通知、递交工作日志、定期收集系统日志等

在终端某个群组添加机器人之后,创建者可以在机器人详情页看的该机器人特有的webhookurl。开发者可以按以下说明a向这个地址发起HTTP POST 请求,即可实现给该群组发送消息。特别特别要注意:一定要保护好机器人的webhook地址,避免泄漏!不要分享到github、博客等可被公开查阅的地方,否则坏人就可以用你的机器人来发垃圾消息了。
以下是用curl工具往群组推送文本消息的示例(注意要将url替换成你的机器人webhook地址,content必须是utf8编码):
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa' \-H 'Content-Type: application/json' \   -d '   {        "msgtype": "text",        "text": {            "content": "hello world"        }   }'
消息类型支持:

简单文本-text,富文本-markdown,文件-file,图片-image,图文-news,以及组合型的卡片类型template_card中的text_notcice和news_notice类。

使用python编写的框架结构如下:
import requestsmywx_url="你的机器人webhook地址"headers = {"Content-Type": "application/json", "charset": "utf-8"}固定data={    "此处为消息内容,后期所有改动都在这里,根据不同的消息类型参考API文档进行编写消息"}respond=requests.post(url=mywx_url,headers=headers,json=data)if respond.json()["errmsg"]=="ok":    print("消息发送成功")else:    print(respond.text)

简单文本类

data={    "msgtype":"text",    "text":{        "content":"时间:"+datetime.datetime.now().strftime('%Y年%m月%d日 %H时%M分%S秒%p')+"\n机器人发来的第一条消息,请查收",        "mentioned_list":["puzhenglin"],#用户ID提醒        "mentioned_mobile_list":["18827340310","@all"]#通过匹配手机号匹配    }}

markdown类

data2={    "msgtype":"markdown",    "markdown":{        "content":"<font color='warning'>时间:</font>"+str(datetime.datetime.now().strftime('%Y年%m月%d日 %H:%M:%S %p')) + "\n"                 "# 爱的回应\n"                  ">\n"                  "<font color='info'>很想和你拥有一个很长很长的未来,很想陪你走完你的一生</font>\n                  "<font color='warning'>彼此温暖,互不辜负</font>\n"                  "@all",    }}


file传输类

先上传到文件上传接口获取media_id才能发送

mywx_url_file="https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=167c4082-8510-4539-87a0-69aecb94f320&type=file" #机器人webhook文件上传接口file={    "file":open(r"E:\今日运维日报.pptx","rb")}media_id=requests.post(url=mywx_url_file,files=file).json()["media_id"]data6={    "msgtype":"file",    "file":{        "media_id":media_id    }}

news类

data3= {    "msgtype": "news",    "news": {        "articles": [            {                "title": "NBA今日新闻\n北京时间11月19日,勇士客场104-89大胜骑士,迎来两连胜",                "descrption": "nba-news",                "url": "https://nbachina.qq.com/article/index.htm?articleId=20211119006852&site=nbatop&column=",                "picurl": "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn21%2F360%2Fw680h480%2F20181112%2F39c7-hnstwwr2260914.jpg&refer=http%3A%2F%2Fn.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639907146&t=444dce80221909b73e5d8e08f77d6997"            }        ]    }}

template_card卡片类-text_notice

days = datetime.datetime(2022, 1, 31, 0, 0, 0) - datetime.datetime.now()#为了效果引用data4 = {    "msgtype": "template_card",    "template_card": {        "card_type": "text_notice",        "main_title": {            "title": "2021倒计时",            "desc": "好好珍惜跟她一起加油!"        },
"emphasis_content": { "title": str(days.days) + "天" + str(days.seconds) + "秒", "desc":"2021倒计时" }, "sub_title_text": "每日一读", "horizontal_content_list": [ { "type": 1, "keyname": "全国热点新闻", "value": "阅读新闻", "url": "http://www.gov.cn/xinwen/yaowen.htm" }, { "type": 1, "keyname": "云南热点新闻", "value": "阅读新闻", "url": "http://www.yn.gov.cn/ywdt/ynyw/" } ], "jump_list": [ { "type": 1, "title": "疫情防控", "url": "https://user.guancha.cn/app/pneumonia" # "appid":"小程序id", } ], "card_action": { "type": 1, "url": "http://finance.yunnan.cn/video/003/009/022/00300902233_vw00000000000001_32bffe39.mp4", }
}
}

template_card卡片类-news_notice

data5 = {    "msgtype": "template_card",    "template_card": {        "card_type": "news_notice",        "main_title": {            "title": "准点NBA-新闻推送",            "desc": "每天准时推送最新勇士队新闻"        },        "card_image": {            "url": "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F11566271830%2F0.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639889007&t=ff5f487bbde84d90d36764cca8871340",            "aspect_ratio": 2.25        },
"horizontal_content_list": [ { "type": 1, "keyname": "今日比赛回顾", "value": "点击观看比赛回放", "url": "https://v.qq.com/x/cover/mzc00200qhithzs/h0041cgb9f9.html" }, { "type": 1, "keyname": "更多NBA赛事", "value": "点击观看直播", "url": "30.tv" } ], "jump_list":[ { "type": 1, "title": "IT资讯",                "url""http://www.cnit5.com/             }], "card_action": { "type": 1, "url": "https://www.bilibili.com/video/BV1644115717?t=3.9", }
}
}

如何让机器人每天定期往群里发最新的新闻、事件、文件?

爬虫(最新数据)+webhook+crond

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
企业微信群机器人使用指南和python示例
什么是webhook
.net开发微信公众平台
《Jenkins从零到壹》Jenkins集成gitlab自动化构建(JAVA 小虚竹 建议收藏)
Google Alert IFTTT 企业微信:收集整合资讯舆情,快人一步
快速获取API状态的三种方式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服