• python给企微发消息


     方法一:webhook方式。使用群机器人给企微群发消息

    1. import requests
    2. def qwxsendmessage(msg):
    3. url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=6c598840-804a-4eb5-a999-a023313' #url换成自己群机器人的webhookurl
    4. data={
    5. 'msgtype':'text',
    6. 'text':{
    7. 'content':msg
    8. }
    9. }
    10. print(data)
    11. res=requests.post(url,json=data)

    消息发送频率限制:每个机器人发送的消息不能超过20条/分钟。

    方法二:使用自建应用的方式

    企微还可以用另一种自建应用的方式,然后通过接口实现单聊发消息,但是自建应用需要配置企业可信任ip。这个配置过程挡住了很多人,最后我也放弃了,没有和企微备案一致的域名。

    下面是使用自建应用发送消息的代码

    1. import requests
    2. import json
    3. #获取token
    4. def qwxget_token():
    5. corpid='wx38bbebfb0834' #corpid换成自己的
    6. corpsecret='OfQLVaaCeJT9vrxini3EYrN71xO8PQzLGFnET' #corpsecret换成自己的
    7. url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='+corpid+'&corpsecret='+corpsecret
    8. response=requests.get(url)
    9. dict_response=response.json()
    10. token=dict_response['access_token']
    11. return token
    12. #发送消息
    13. def qwxmessage(token):
    14. data={
    15. 'touser':'PengXuan', #成员编号换成自己的
    16. 'msgtype':'text',
    17. 'agentid':1000033, #应用id换成自己的
    18. 'text':{
    19. 'content':'我就试一下'
    20. },
    21. 'safe':0
    22. }
    23. url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='+token
    24. res=requests.post(url,data=json.dumps(data,ensure_ascii=False).encode('utf-8')).json()
    25. print(res)
    26. #调用
    27. token=qwxget_token()
    28. qwxmessage(token)

    执行调用后,返回错误代码

     {'errcode': 60020, 'errmsg': 'not allow to access from your ip, hint: [1709260518618543373180730], from ip: 27.188.36.174, more info at https://open.work.weixin.qq.com/devtool/query?e=60020'}

    经过查询企微开放平台的错误码

    错误码:60020

    不安全的访问IP。请根据调用的应用类型分别按如下方法确认:
    1)若调用者是企业自建应用或通讯录同步助手,请确认该IP是本企业服务器IP,并已经配置到应用详情的“企业可信IP”项目中。第三方服务商IP不能调用。
    2)若调用者是第三方应用或服务商代开发应用,请确认该IP已经配置到“服务商管理后台”-“服务商信息”-“基本信息”-“IP白名单”。
    3) 配置完可信IP之后,需要1分钟后才生效。

    上面这个配置最后我也没有完成,后面有机会再试吧。

  • 相关阅读:
    来说说ThreadLocal内存溢出问题
    macOS上制作arm64的jdk17镜像
    def和class的区别
    Python语言学习实战-内置函数property()的使用(附源码和实现效果)
    Istio-PilotAgent源码分析
    Word控件Spire.Doc 【段落处理】教程(十三):如何在 C#、VB.NET 中将 RTF 字符串插入 Word 文档
    微信小程序 限制字数文本域框组件封装
    英伟达jetson硬件(NX,nano,AGX,TX1,TX2)通用开机自动开启风扇教程
    中断实验(按键/光电开关/火焰传感器/人体红外)
    【2022】【论文笔记】基于激光直写氧化石墨烯纸的超薄THz偏转——
  • 原文地址:https://blog.csdn.net/pengxuan/article/details/136388295