訂閱號服務開發-04主動消息推送


@

客服消息接口

微信提供客服接口,用戶給用戶發送消息。在 wetchat.py 中封裝成WeChatMessage
注意:未認證的訂閱號和公眾號,是沒有該接口權限的

發送文本消息

發送消息前,先獲取粉絲的open_id,這里取巧,直接在公眾號后台拿
在這里插入圖片描述

wx.message.send_text(user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",content="我不在我不在我不在.....")
Out[25]: {'errcode': 0, 'errmsg': 'ok'}
wx.message.send_text(user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",content="我不在我不在我不在.....")
Out[26]: {'errcode': 0, 'errmsg': 'ok'}

效果如下:

在這里插入圖片描述

[danger] 注意,會話需要由用戶先發起,否則會出現45015:回復時間超過限制的錯誤。

發送圖片消息

wx.message.send_image(user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",media_id="BzzIfj9NcYYTuaPR_j133jbk4KxYuPvhoZx68usaDi0")
Out[28]: {'errcode': 0, 'errmsg': 'ok'}

效果如下:
在這里插入圖片描述

發送圖文消息

  • 通過永久圖文素材media發送
wx.message.send_articles(
    user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",
    articles="BzzIfj9NcYYTuaPR_j133mW-veyn3sNRANnduEtdnss"
)
Out[29]: {'errcode': 0, 'errmsg': 'ok'}

在這里插入圖片描述

  • 點擊跳轉到外鏈
wx.message.send_articles(
    user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",
    articles=[
        {
            "title": "Happy Day",
            "description": "Is Really A Happy Day",
            "url": "https://www.kancloud.cn/@guanfuchang",
            "picurl": "http://mmbiz.qpic.cn/mmbiz_png/QuFmLYzPJ2DK93uoeUAibJSqQBmgeR1uhdicrQKxT3QzZVdfWUIHa6fmFfuaibLNibqYGVHQcwyzCbsbPWTN82Vs9g/0?wx_fmt=png"
        }
    ]
)
Out[30]: {'errcode': 0, 'errmsg': 'ok'}

在這里插入圖片描述

  • 通過臨時圖文素材media_id發送
wx.message.send_articles(user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",
                                    articles=wx.media.upload_articles(
                                        articles=[
                                            {
                                                "thumb_media_id": wx.media.upload("image", open("D://666.png", "rb")).get("media_id"),
                                                "author": "觀小魚",
                                                "title": "圖文標題",
                                                "content_source_url": "https://www.kancloud.cn/@guanfuchang",
                                                "content": "圖文詳情內容",
                                                "digest": "圖文概要描述",
                                                "show_cover_pic": 1,
                                                "need_open_comment": 1,
                                                "only_fans_can_comment": 1
                                            }
                                        ]).get("media_id"))
Out[32]: {'errcode': 0, 'errmsg': 'ok'}

在這里插入圖片描述

發送音樂消息

wx.message.send_music(
        user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",
        url="https://od.qingting.fm/m4a/5a8e82757cb89146f20a287b_8762416_64.m4a",
        hq_url="https://od.qingting.fm/m4a/5a8e82757cb89146f20a287b_8762416_64.m4a",
        thumb_media_id=wx.media.upload("thumb", open("D://music.jpg","rb")).get("thumb_media_id"),
        title="夢想明月曲",
        description="崔子格",
    )
Out[35]: {'errcode': 0, 'errmsg': 'ok'}

發送視頻消息

wx.message.send_video(
        user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",
        media_id="BzzIfj9NcYYTuaPR_j133ietkr1Oy2009whI6vdTLR8",
    )
Out[36]: {'errcode': 0, 'errmsg': 'ok'}

群發消息接口

群發文本

wx.message.send_mass_text(
    group_or_users=None,
    content="群發消息",
    is_to_all=True
)
Out[37]: {'errcode': 0, 'errmsg': 'send job submission success', 'msg_id': 1000000002}

[info] 更多群發接口就不一一舉例,參考源代碼 message 類中,send_mass_xxx 方法即可。


:-:
微信掃一掃,關注“python測試開發圈”,獲取更多測試開發分享!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM