如何使用企業(yè)群機(jī)器人

如何使用群機(jī)器人

  • 在終端某個(gè)群組添加機(jī)器人之后,創(chuàng)建者可以在機(jī)器人詳情頁看的該機(jī)器人特有的webhookurl。開發(fā)者可以按以下說明a向這個(gè)地址發(fā)起HTTP POST 請求,即可實(shí)現(xiàn)給該群組發(fā)送消息。下面舉個(gè)簡單的例子.
    假設(shè)webhook是:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa

特別特別要注意:一定要保護(hù)好機(jī)器人的webhook地址,避免泄漏!不要分享到github、博客等可被公開查閱的地方,否則壞人就可以用你的機(jī)器人來發(fā)垃圾消息了。

以下是用curl工具往群組推送文本消息的示例(注意要將url替換成你的機(jī)器人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"        }   }'
  • 當(dāng)前自定義機(jī)器人支持文本(text)、markdown(markdown)、圖片(image)、圖文(news)四種消息類型。
  • 機(jī)器人的text/markdown類型消息支持在content中使用<@userid>擴(kuò)展語法來@群成員

消息類型及數(shù)據(jù)格式

文本類型

{    "msgtype": "text",    "text": {        "content": "廣州今日天氣:29度,大部分多云,降雨概率:60%",        "mentioned_list":["wangqing","@all"],        "mentioned_mobile_list":["13800001111","@all"]    }}
參數(shù) 是否必填 說明
msgtype 消息類型,此時(shí)固定為text
content 文本內(nèi)容,最長不超過2048個(gè)字節(jié),必須是utf8編碼
mentioned_list userid的列表,提醒群中的指定成員(@某個(gè)成員),@all表示提醒所有人,如果開發(fā)者獲取不到userid,可以使用mentioned_mobile_list
mentioned_mobile_list 手機(jī)號(hào)列表,提醒手機(jī)號(hào)對應(yīng)的群成員(@某個(gè)成員),@all表示提醒所有人
img

markdown類型

{    "msgtype": "markdown",    "markdown": {        "content": "實(shí)時(shí)新增用戶反饋<font color=\"warning\">132例</font>,請相關(guān)同事注意。\n         >類型:<font color=\"comment\">用戶反饋</font>         >普通用戶反饋:<font color=\"comment\">117例</font>         >VIP用戶反饋:<font color=\"comment\">15例</font>"    }}
參數(shù) 是否必填 說明
msgtype 消息類型,此時(shí)固定為markdown
content markdown內(nèi)容,最長不超過4096個(gè)字節(jié),必須是utf8編碼
img

目前支持的markdown語法是如下的子集:

  1. 標(biāo)題 (支持1至6級(jí)標(biāo)題,注意#與文字中間要有空格)

    # 標(biāo)題一## 標(biāo)題二### 標(biāo)題三#### 標(biāo)題四##### 標(biāo)題五###### 標(biāo)題六
    
  2. 加粗

    **bold**
    
  3. 鏈接

    [這是一個(gè)鏈接](http://work.weixin.qq.com/api/doc)
    
  4. 行內(nèi)代碼段(暫不支持跨行)

    `code`
    
  5. 引用

    > 引用文字
    
  6. 字體顏色(只支持3種內(nèi)置顏色)

    <font color="info">綠色</font><font color="comment">灰色</font><font color="warning">橙紅色</font>
    

例如

        message = "## **【西點(diǎn)云】2022年2月17日 日報(bào)**\n         " \
                  ">100| 線上續(xù)款 50單 5100元 \n  " \
                  ">101| 線下消費(fèi) 50單 5100元 \n  " \
                  "><font color=\"red\">102|</font><font color=\"red\"> 線下消費(fèi)</font><font color=\"red\"> 1單(ERP 2單)</font><font color=\"red\"> 100元(ERP 99元)</font> \n" \
                  "<font color=\"#930000\">**提示:紅色字體表示賬務(wù)不一致,請核實(shí)!**</font>"
企業(yè)微信截圖_683a4f27-3438-4b24-9c25-0143567ca194.png

圖片類型

{    "msgtype": "image",    "image": {        "base64": "DATA",        "md5": "MD5"    }}
參數(shù) 是否必填 說明
msgtype 消息類型,此時(shí)固定為image
base64 圖片內(nèi)容的base64編碼
md5 圖片內(nèi)容(base64編碼前)的md5值

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

img

圖文類型

{    "msgtype": "news",    "news": {       "articles" : [           {               "title" : "中秋節(jié)禮品領(lǐng)取",               "description" : "今年中秋節(jié)公司有豪禮相送",               "url" : "www.qq.com",               "picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"           }        ]    }}
參數(shù) 是否必填 說明
msgtype 消息類型,此時(shí)固定為news
articles 圖文消息,一個(gè)圖文消息支持1到8條圖文
title 標(biāo)題,不超過128個(gè)字節(jié),超過會(huì)自動(dòng)截?cái)?/td>
description 描述,不超過512個(gè)字節(jié),超過會(huì)自動(dòng)截?cái)?/td>
url 點(diǎn)擊后跳轉(zhuǎn)的鏈接。
picurl 圖文消息的圖片鏈接,支持JPG、PNG格式,較好的效果為大圖 1068455,小圖150150。
img

文件類型

{    "msgtype": "file",    "file": {         "media_id": "3a8asd892asd8asd"    }}
參數(shù) 是否必填 說明
msgtype 消息類型,此時(shí)固定為file
media_id 文件id,通過下文的文件上傳接口獲取
img

模版卡片類型

文本通知模版卡片

img
{    "msgtype":"template_card",    "template_card":{        "card_type":"text_notice",        "source":{            "icon_url":"https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0",            "desc":"企業(yè)微信"        },        "main_title":{            "title":"歡迎使用企業(yè)微信",            "desc":"您的好友正在邀請您加入企業(yè)微信"        },        "emphasis_content":{            "title":"100",            "desc":"數(shù)據(jù)含義"        },        "sub_title_text":"下載企業(yè)微信還能搶紅包!",        "horizontal_content_list":[            {                "keyname":"邀請人",                "value":"張三"            },            {                "keyname":"企微官網(wǎng)",                "value":"點(diǎn)擊訪問",                "type":1,                "url":"https://work.weixin.qq.com/?from=openApi"            },            {                "keyname":"企微下載",                "value":"企業(yè)微信.apk",                "type":2,                "media_id":"MEDIAID"            }        ],        "jump_list":[            {                "type":1,                "url":"https://work.weixin.qq.com/?from=openApi",                "title":"企業(yè)微信官網(wǎng)"            },            {                "type":2,                "appid":"APPID",                "pagepath":"PAGEPATH",                "title":"跳轉(zhuǎn)小程序"            }        ],        "card_action":{            "type":1,            "url":"https://work.weixin.qq.com/?from=openApi",            "appid":"APPID",            "pagepath":"PAGEPATH"        }    }}

請求參數(shù)

參數(shù) 類型 必須 說明
msgtype String 消息類型,此時(shí)的消息類型固定為template_card
template_card Object 具體的模版卡片參數(shù)

template_card的參數(shù)說明

類型 必須 說明
card_type String 模版卡片的模版類型,文本通知模版卡片的類型為text_notice
source Object 卡片來源樣式信息,不需要來源樣式可不填寫
source.icon_url String 來源圖片的url
source.desc String 來源圖片的描述,建議不超過13個(gè)字
main_title Object 模版卡片的主要內(nèi)容,包括一級(jí)標(biāo)題和標(biāo)題輔助信息
main_title.title String 一級(jí)標(biāo)題,建議不超過26個(gè)字
main_title.desc String 標(biāo)題輔助信息,建議不超過30個(gè)字
emphasis_content Object 關(guān)鍵數(shù)據(jù)樣式
emphasis_content.title String 關(guān)鍵數(shù)據(jù)樣式的數(shù)據(jù)內(nèi)容,建議不超過10個(gè)字
emphasis_content.desc String 關(guān)鍵數(shù)據(jù)樣式的數(shù)據(jù)描述內(nèi)容,建議不超過15個(gè)字
sub_title_text String 二級(jí)普通文本,建議不超過112個(gè)字
horizontal_content_list Object[] 二級(jí)標(biāo)題+文本列表,該字段可為空數(shù)組,但有數(shù)據(jù)的話需確認(rèn)對應(yīng)字段是否必填,列表長度不超過6
horizontal_content_list.type Int 鏈接類型,0或不填代表是普通文本,1 代表跳轉(zhuǎn)url,2 代表下載附件
horizontal_content_list.keyname String 二級(jí)標(biāo)題,建議不超過5個(gè)字
horizontal_content_list.value String 二級(jí)文本,如果horizontal_content_list.type是2,該字段代表文件名稱(要包含文件類型),建議不超過26個(gè)字
horizontal_content_list.url String 鏈接跳轉(zhuǎn)的url,horizontal_content_list.type是1時(shí)必填
horizontal_content_list.media_id String 附件的media_id,horizontal_content_list.type是2時(shí)必填
jump_list Object[] 跳轉(zhuǎn)指引樣式的列表,該字段可為空數(shù)組,但有數(shù)據(jù)的話需確認(rèn)對應(yīng)字段是否必填,列表長度不超過3
jump_list.type Int 跳轉(zhuǎn)鏈接類型,0或不填代表不是鏈接,1 代表跳轉(zhuǎn)url,2 代表跳轉(zhuǎn)小程序
jump_list.title String 跳轉(zhuǎn)鏈接樣式的文案內(nèi)容,建議不超過13個(gè)字
jump_list.url String 跳轉(zhuǎn)鏈接的url,jump_list.type是1時(shí)必填
jump_list.appid String 跳轉(zhuǎn)鏈接的小程序的appid,jump_list.type是2時(shí)必填
jump_list.pagepath String 跳轉(zhuǎn)鏈接的小程序的pagepath,jump_list.type是2時(shí)選填
card_action Object 整體卡片的點(diǎn)擊跳轉(zhuǎn)事件,text_notice模版卡片中該字段為必填項(xiàng)
card_action.type Int 卡片跳轉(zhuǎn)類型,0或不填代表不是鏈接,1 代表跳轉(zhuǎn)url,2 代表打開小程序。text_notice模版卡片中該字段取值范圍為[1,2]
card_action.url String 跳轉(zhuǎn)事件的url,card_action.type是1時(shí)必填
card_action.appid String 跳轉(zhuǎn)事件的小程序的appid,card_action.type是2時(shí)必填
card_action.pagepath String 跳轉(zhuǎn)事件的小程序的pagepath,card_action.type是2時(shí)選填

圖文展示模版卡片

img
{    "msgtype":"template_card",    "template_card":{        "card_type":"news_notice",        "source":{            "icon_url":"https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0",            "desc":"企業(yè)微信"        },        "main_title":{            "title":"歡迎使用企業(yè)微信",            "desc":"您的好友正在邀請您加入企業(yè)微信"        },        "card_image":{            "url":"https://wework.qpic.cn/wwpic/354393_4zpkKXd7SrGMvfg_1629280616/0",            "aspect_ratio":2.25        },        "vertical_content_list":[            {                "title":"驚喜紅包等你來拿",                "desc":"下載企業(yè)微信還能搶紅包!"            }        ],        "horizontal_content_list":[            {                "keyname":"邀請人",                "value":"張三"            },            {                "keyname":"企微官網(wǎng)",                "value":"點(diǎn)擊訪問",                "type":1,                "url":"https://work.weixin.qq.com/?from=openApi"            },            {                "keyname":"企微下載",                "value":"企業(yè)微信.apk",                "type":2,                "media_id":"MEDIAID"            }        ],        "jump_list":[            {                "type":1,                "url":"https://work.weixin.qq.com/?from=openApi",                "title":"企業(yè)微信官網(wǎng)"            },            {                "type":2,                "appid":"APPID",                "pagepath":"PAGEPATH",                "title":"跳轉(zhuǎn)小程序"            }        ],        "card_action":{            "type":1,            "url":"https://work.weixin.qq.com/?from=openApi",            "appid":"APPID",            "pagepath":"PAGEPATH"        }    }}

請求參數(shù)

參數(shù) 類型 必須 說明
msgtype String 模版卡片的消息類型為template_card
template_card Object 具體的模版卡片參數(shù)

template_card的參數(shù)說明

類型 必須 說明
card_type String 模版卡片的模版類型,圖文展示模版卡片的類型為news_notice
source Object 卡片來源樣式信息,不需要來源樣式可不填寫
source.icon_url String 來源圖片的url
source.desc String 來源圖片的描述,建議不超過13個(gè)字
main_title Object 模版卡片的主要內(nèi)容,包括一級(jí)標(biāo)題和標(biāo)題輔助信息
main_title.title String 一級(jí)標(biāo)題,建議不超過26個(gè)字
main_title.desc String 標(biāo)題輔助信息,建議不超過30個(gè)字
card_image Object 圖片樣式
card_image.url Object 圖片的url
card_image.aspect_ratio Float 圖片的寬高比,寬高比要小于2.25,大于1.3,不填該參數(shù)默認(rèn)1.3
vertical_content_list Object[] 卡片二級(jí)垂直內(nèi)容,該字段可為空數(shù)組,但有數(shù)據(jù)的話需確認(rèn)對應(yīng)字段是否必填,列表長度不超過4
vertical_content_list.title String 卡片二級(jí)標(biāo)題,建議不超過26個(gè)字
vertical_content_list.desc String 二級(jí)普通文本,建議不超過112個(gè)字
horizontal_content_list Object[] 二級(jí)標(biāo)題+文本列表,該字段可為空數(shù)組,但有數(shù)據(jù)的話需確認(rèn)對應(yīng)字段是否必填,列表長度不超過6
horizontal_content_list.type Int 模版卡片的二級(jí)標(biāo)題信息內(nèi)容支持的類型,1是url,2是文件附件
horizontal_content_list.keyname String 二級(jí)標(biāo)題,建議不超過5個(gè)字
horizontal_content_list.value String 二級(jí)文本,如果horizontal_content_list.type是2,該字段代表文件名稱(要包含文件類型),建議不超過26個(gè)字
horizontal_content_list.url String 鏈接跳轉(zhuǎn)的url,horizontal_content_list.type是1時(shí)必填
horizontal_content_list.media_id String 附件的media_id,horizontal_content_list.type是2時(shí)必填
jump_list Object[] 跳轉(zhuǎn)指引樣式的列表,該字段可為空數(shù)組,但有數(shù)據(jù)的話需確認(rèn)對應(yīng)字段是否必填,列表長度不超過3
jump_list.type Int 跳轉(zhuǎn)鏈接類型,0或不填代表不是鏈接,1 代表跳轉(zhuǎn)url,2 代表跳轉(zhuǎn)小程序
jump_list.title String 跳轉(zhuǎn)鏈接樣式的文案內(nèi)容,建議不超過13個(gè)字
jump_list.url String 跳轉(zhuǎn)鏈接的url,jump_list.type是1時(shí)必填
jump_list.appid String 跳轉(zhuǎn)鏈接的小程序的appid,jump_list.type是2時(shí)必填
jump_list.pagepath String 跳轉(zhuǎn)鏈接的小程序的pagepath,jump_list.type是2時(shí)選填
card_action Object 整體卡片的點(diǎn)擊跳轉(zhuǎn)事件,news_notice模版卡片中該字段為必填項(xiàng)
card_action.type Int 卡片跳轉(zhuǎn)類型,0或不填代表不是鏈接,1 代表跳轉(zhuǎn)url,2 代表打開小程序。news_notice模版卡片中該字段取值范圍為[1,2]
card_action.url String 跳轉(zhuǎn)事件的url,card_action.type是1時(shí)必填
card_action.appid String 跳轉(zhuǎn)事件的小程序的appid,card_action.type是2時(shí)必填
card_action.pagepath String 跳轉(zhuǎn)事件的小程序的pagepath,card_action.type是2時(shí)選填

消息發(fā)送頻率限制

每個(gè)機(jī)器人發(fā)送的消息不能超過20條/分鐘。

文件上傳接口

素材上傳得到media_id,該media_id僅三天內(nèi)有效
media_id只能是對應(yīng)上傳文件的機(jī)器人可以使用

請求方式:POST(HTTPS
請求地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=KEY&type=TYPE

使用multipart/form-data POST上傳文件, 文件標(biāo)識(shí)名為”media”
參數(shù)說明:

參數(shù) 必須 說明
key 調(diào)用接口憑證, 機(jī)器人webhookurl中的key參數(shù)
type 固定傳file

POST的請求包中,form-data中媒體文件標(biāo)識(shí),應(yīng)包含有 filename、filelength、content-type等信息

filename標(biāo)識(shí)文件展示的名稱。比如,使用該media_id發(fā)消息時(shí),展示的文件名由該字段控制

請求示例:

POST https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa&type=file HTTP/1.1Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468Content-Length: 220---------------------------acebdf13572468Content-Disposition: form-data; name="media";filename="wework.txt"; filelength=6Content-Type: application/octet-streammytext---------------------------acebdf13572468--

返回?cái)?shù)據(jù):

{   "errcode": 0,   "errmsg": "ok",   "type": "file",   "media_id": "1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0",   "created_at": "1380000000"}

參數(shù)說明:

參數(shù) 說明
type 媒體文件類型,分別有圖片(image)、語音(voice)、視頻(video),普通文件(file)
media_id 媒體文件上傳后獲取的唯一標(biāo)識(shí),3天內(nèi)有效
created_at 媒體文件上傳時(shí)間戳

上傳的文件限制:

  • 要求文件大小在5B~20M之間
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用到的組件 1、通過CocoaPods安裝 2、第三方類庫安裝 3、第三方服務(wù) 友盟社會(huì)化分享組件 友盟用戶反饋 ...
    SunnyLeong閱讀 15,203評(píng)論 1 180
  • LayUI 主要內(nèi)容 LayUI 的安裝及使用 LayUI 的介紹 ? layui(諧音:類UI) 是一款采用自身...
    一紙油傘閱讀 1,348評(píng)論 0 0
  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者,不喜歡去冒險(xiǎn),但是人生放棄了冒險(xiǎn),也就放棄了無數(shù)的可能。 ...
    yichen大刀閱讀 8,094評(píng)論 0 4
  • 公元:2019年11月28日19時(shí)42分農(nóng)歷:二零一九年 十一月 初三日 戌時(shí)干支:己亥乙亥己巳甲戌當(dāng)月節(jié)氣:立冬...
    石放閱讀 7,510評(píng)論 0 2

友情鏈接更多精彩內(nèi)容