1. 在釘釘和微信群助手中,添加智能機器人(選擇自定義機器人)

(釘釘) (微信)
2. 獲取webhook地址
一般如下格式:
https://oapi.dingtalk.com/robot/send?access_token=123abc(釘釘)
https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=123abc(微信)
群發消息的本質,即向這個webhook地址發送http請求(post),發送請求時,必須將字符集編碼設置成UTF-8
3. 釘釘中需要進行安全設置
可以自定義關鍵詞:最多可以設置10個關鍵詞,消息中至少包含其中1個關鍵詞才可以發送成功
微信無此限制
4. 支持格式如下:
釘釘:支持文本 (text)、鏈接 (link)、markdown(markdown)、ActionCard、FeedCard消息類型
微信:支持文本、markdown、圖片、圖文
5. 可以編寫代碼發送群消息啦~
以發送文本消息為例:
import requests import json # 機器人基類 class RobotBase: def __init__(self): self.__headers = {'Content-Type': 'application/json;charset=utf-8'} self.url = '' def send_msg(self,text): json_text = { "msgtype": "text", "text": { "content": text }, "at": { "atMobiles": [ "" ], "isAtAll": True } } return requests.post(self.url, json.dumps(json_text), headers=self.__headers).content # 機器人子類 - 釘釘機器人 class RobotDingtalk(RobotBase): def __init__(self): super().__init__() # 填寫釘釘機器人的url self.url = 'https://oapi.dingtalk.com/robot/send?access_token=123abc' # 機器人子類 - 微信機器人 class RobotWeixin(RobotBase): def __init__(self): super().__init__() # 填寫微信機器人的url self.url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=123-abc' if __name__ == '__main__': robot_ding = RobotDingtalk() robot_ding.send_msg('hello dingding') # 向釘釘群發消息 robot_weixin = RobotWeixin() robot_weixin.send_msg('hello weixin') # 向微信群發消息
以上是艾特所有人,如果要艾特指定人,如下:
# 機器人基類 class RobotBase: def __init__(self): self.__headers = {'Content-Type': 'application/json;charset=utf-8'} self.url = '' def send_msg(self,text): json_text = { "msgtype": "text", "text": { "content": text }, "at": { "atMobiles": [ "17765006069" # 艾特指定人(手機號) ], "isAtAll": False # 不艾特所有人 } } return requests.post(self.url, json.dumps(json_text), headers=self.__headers).content
6. 開發文檔
釘釘機器人開發文檔:https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq/26eaddd5
微信機器人開發文檔:在企業微信群機器人配置頁面(配置說明中)
.
