企業微信羣機器人使用指南和python示例

流程簡述

目前只支持發送數據,無法進行交互。對於監控和報告類任務,比如監控機器性能和一些指數等工作,可以極大簡化工作量,末尾有簡單的python示例。

在終端某個羣組成功添加機器人之後,可以獲取到webhook地址。羣機器人是每個成員都可以創建的。

創建者按以下說明構造post請求的data向這個webhook發起HTTP POST請求,即可實現給該羣組發送消息。

假設webhook是:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=633a31f6-7f9c-4bc4-97a0-0ec1eefa589

curl發送示例(注意要將url替換成你的機器人webhook地址,content必須是utf8編碼):

curl 'http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=633a31f6-7f9c-4bc4-97a0-0ec1eefa5898' \
   -H 'Content-Type: application/json' \
   -d '
   {
        "msgtype": "text",
        "text": {
            "content": "hello world"
        }
   }'

消息類型及數據格式

文本類型

{
    "msgtype": "text",
    "text": {
        "content": "廣州今日天氣:29度,大部分多雲,降雨概率:60%",
        "mentioned_list":["wangqing","@all"],
        "mentioned_mobile_list":["13800001111","@all"]
    }
}
參數 必須 說明
msgtype true 消息類型,此時固定爲text
content true 文本內容,最長不超過2048個字節,必須是utf8編碼
mentioned_list false userid的列表,提醒羣中的指定成員(@某個成員),@all表示提醒所有人。
mentioned_mobile_list false 手機號列表,提醒手機號對應的羣成員(@某個成員),@all表示提醒所有人

mentioned_list中的ID就是企業微信的郵箱。

markdown類型

{
    "msgtype": "markdown",
    "markdown": {
        "content": "實時新增用戶反饋<font color=\"warning\">132例</font>,請相關同事注意。\n
         >類型:<font color=\"comment\">用戶反饋</font> \n
         >普通用戶反饋:<font color=\"comment\">117例</font> \n
         >VIP用戶反饋:<font color=\"comment\">15例</font>"
    }
}
參數 必須 說明
msgtype true 消息類型,此時固定爲markdown
content true markdown內容,最長不超過4096個字節,必須是utf8編碼

目前支持markdown如下語法:

標題 (支持1至6級標題,注意#與文字中間要有空格)

# 標題一
## 標題二
### 標題三
#### 標題四
##### 標題五
###### 標題六

加粗

**bold**

鏈接

[這是一個鏈接](http://work.weixin.qq.com/api/doc)

行內代碼段(暫不支持跨行)

`code`

引用

> 引用文字
字體顏色(只支持3種內置顏色)
<font color="info">綠色</font>
<font color="comment">灰色</font>
<font color="warning">橙紅色</font>

圖片類型

{
    "msgtype": "image",
    "image": {
        "base64": "DATA",
        "md5": "MD5"
    }
}
參數 必須 說明
msgtype true 消息類型,此時固定爲image
base64 true 圖片內容的base64編碼
md5 true 圖片內容(base64編碼前)的md5值

注:圖片(base64編碼前)最大不能超過2M,支持JPG,PNG格式

圖文類型

{
    "msgtype": "news",
    "news": {
       "articles" : [
           {
               "title" : "中秋節禮品領取",
               "description" : "今年中秋節公司有豪禮相送",
               "url" : "URL",
               "picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
           }
        ]
    }
}
參數 必須 說明
msgtype true 消息類型,此時固定爲news
articles true 圖文消息,一個圖文消息支持1到8條圖文
title true 標題,不超過128個字節,超過會自動截斷
description false 描述,不超過512個字節,超過會自動截斷
url true 點擊後跳轉的鏈接。
picurl false 圖文消息的圖片鏈接,支持JPG、PNG格式,較好的效果爲大圖 1068455,小圖150150。

消息發送頻率限制

每個機器人發送的消息不能超過20條/分鐘。

python示例

import requests

url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=633a31f6-7f9c-4bc4-97a0-0ec1eefa589"
headers = {"Content-Type": "text/plain"}
s = "What do you want to say? "
data = {
      "msgtype": "text",
      "text": {
         "content": s,
      }
   }
r = requests.post(url, headers=headers, json=data)
print(r.text)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章