讀古今文學網 > 微信公眾平台開發:從零基礎到ThinkPHP5高性能框架實踐 > 20.7.4 主動發送消息 >

20.7.4 主動發送消息

對於消息型應用,支持文本、圖片、語音、視頻、文件、圖文、素材圖文等消息類型。主頁型應用只支持文本消息類型,且文本長度不超過20個字。企業微信還支持文本卡片消息。

主動發送消息的接口如下。


https:// qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN
  

主動發送消息時,POST數據示例如下。

1)文本消息類型的數據為:


{
    "touser":"UserID1|UserID2|UserID3",
    "toparty":" PartyID1 | PartyID2 ",
    "totag":" TagID1 | TagID2 ",
    "msgtype":"text",
    "agentid":1,
    "text":{
        "content":"Holiday Request For Pony(http:// xxxxx)"
    },
    "safe":0
}
  

2)圖片消息類型的數據為:


{
    "touser":"UserID1|UserID2|UserID3",
    "toparty":" PartyID1 | PartyID2 ",
    "totag":" TagID1 | TagID2 ",
    "msgtype":"image",
    "agentid":1,
    "image":{
        "media_id":"MEDIA_ID"
    },
    "safe":0
}
  

3)語音消息類型的數據為:


{
    "touser":"UserID1|UserID2|UserID3",
    "toparty":" PartyID1 | PartyID2 ",
    "totag":" TagID1 | TagID2 ",
    "msgtype":"voice",
    "agentid":1,
    "voice":{
        "media_id":"MEDIA_ID"
    },
    "safe":0
}
  

4)視頻消息類型的數據為:


{
    "touser":"UserID1|UserID2|UserID3",
    "toparty":" PartyID1 | PartyID2 ",
    "totag":" TagID1 | TagID2 ",
    "msgtype":"video",
    "agentid":1,
    "video":{
        "media_id":"MEDIA_ID",
        "title":"Title",
        "description":"Description"
    },
    "safe":0
}
  

5)文件消息類型的數據為:


{
    "touser":"UserID1|UserID2|UserID3",
    "toparty":" PartyID1 | PartyID2 ",
    "totag":" TagID1 | TagID2 ",
    "msgtype":"file",
    "agentid":1,
    "file":{
        "media_id":"MEDIA_ID"
    },
    "safe":"0"
}
  

6)圖文消息類型的數據為:


{
    "touser":"UserID1|UserID2|UserID3",
    "toparty":" PartyID1 | PartyID2 ",
    "totag":" TagID1 | TagID2 ",
    "msgtype":"news",
    "agentid":1,
    "news":{
        "articles":[
            {
                "title":"Title",
                "description":"Description",
                "url":"URL",
                "picurl":"PIC_URL"
            },
            {
                "title":"Title",
                "description":"Description",
                "url":"URL",
                "picurl":"PIC_URL"
            }
        ]
    }
}
  

7)素材圖文消息類型的數據為:


{
    "touser":"UserID1|UserID2|UserID3",
    "toparty":" PartyID1 | PartyID2 ",
    "totag":" TagID1 | TagID2 ",
    "msgtype":"mpnews",
    "agentid":1,
    "mpnews":{
        "articles":[
            {
                "title":"Title",
                "thumb_media_id":"id",
                "author":"Author",
                "content_source_url":"URL",
                "content":"Content",
                "digest":"Digest description",
                "show_cover_pic":"0"
            },
            {
                "title":"Title",
                "thumb_media_id":"id",
                "author":"Author",
                "content_source_url":"URL",
                "content":"Content",
                "digest":"Digest description",
                "show_cover_pic":"0"
            }
        ]
    }
}
  

8)文本卡片消息類型的數據為:


{
    "touser":"UserID1|UserID2|UserID3",
    "toparty":" PartyID1 | PartyID2 ",
    "msgtype":"textcard",
    "agentid":1,
    "textcard":{
        "title":"領獎通知",
        "description":"<p>2016年9月26日</p> <p>
        恭喜你抽中iPhone 7一台,領獎碼:xxxx</p><p>請於2016年10月
        10日前聯繫行政同事領取</p>",
        "url":"URL"
    }
}
  

上述數據的參數說明如表20-36所示。

表20-36 主動發送消息接口的參數說明

正確提交時,返回的數據示例如下。


{"errcode":0,"errmsg":"ok"}