Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to realize the function of sending messages to WeCom

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to use Python to send messages to WeCom? for this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

I. Overview

Here is how to use python3 to send a message to WeCom. My environment is linux + python3.6.10.

II. Python script #! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 12 coding @ Author: wangying# @ Site: # @ File: WeCom .py # @ Software: PyCharm #! / root/.virtualenvs/wechat/bin/python# usage: send message via wechat import requests, sys Jsonimport urllib3urllib3.disable_warnings () # fill in the parameter # Corpid is the identification of the enterprise ID Corpid = "ww32a580fceb30f350" # Secret is the administrative group credential key Secret = "QyBTbg1QL9lmNwaErqv1DMV0Y_G3lZre268E0hpTDqk" # Application IDAgentid = "1000004" # token_config file placement path Token_config = r _ code _ Secret): "get access_token"Url =" https://qyapi.weixin.qq.com/cgi-bin/gettoken" Data = {"corpid": Corpid, "corpsecret": Secret} r = requests.get (url=Url, params=Data) Verify=False) print (r.json ()) if r.json () ['errcode']! = 0: return False else: Token = r.json () [' access_token'] file = open (Token_config,'w') file.write (r.text) file.close () return Token def SendMessage (Partyid, Subject Content): "" send message "" # get token information try: file = open (Token_config,'r') Token = json.load (file) ['access_token'] file.close () except: Token = GetTokenFromServer (Corpid) Secret) # send messages Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"% Token Data = {" toparty ": Partyid," msgtype ":" text "," agentid ": Agentid," text ": {" content ": Subject +'\ n' + Content}," safe ":" 0 "} r = requests.post (url=Url Data=json.dumps (Data), verify=False) # if sending fails Will retry three times n = 1 while r.json () ['errcode']! = 0 and n

< 4: n = n + 1 Token = GetTokenFromServer(Corpid, Secret) if Token: Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token r = requests.post(url=Url, data=json.dumps(Data), verify=False) print(r.json()) return r.json() if __name__ == '__main__': # 部门id Partyid = '20' # 消息标题 Subject = '自应用程序代码测试' # 消息内容 Content = 'str(sys.argv[3])' Status = SendMessage(Partyid, Subject, Content) print(Status) 需要修改的地方,其中Corpid,Secret,Agentid我会在后面的截图指出来要填的值。 下行是linux环境下python3的可执行路径,如果是windows,那么这行就不需要,只要将python的路径加入到path即可 #!/root/.virtualenvs/wechat/bin/python # Corpid是企业ID Corpid = "ww34d7fed41a8d5dxx" # Secret是管理组凭证密钥 Secret = "8QwzOi0xCvbp1ZnCbnQJ1d6uEIv-Lrbcjp93UrHFxxx" # Agentid是应用ID Agentid = "1000002" # Token_config里面存放token信息,这个自己随便填就行,只要自己有写的权限 Token_config = r'/tmp/zabbix_wechat_config.json' 三、企业微信设置1. 注册企业微信 https://qy.weixin.qq.com/

two。 Click to enter the management background

Create an application

3. After the application is created

Agentid,Secret came out.

4. View Enterprise id

This is the Corpid in the script.

5. View department id

My python script is sent in groups through the department id, so it will be useful for this department to id later.

IV. Test script

Run directly: python manage.py runserver

This is the answer to the question about how to use Python to send messages to WeCom. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report