python调用企业微信接口发送群聊消息代码参考
生活随笔
收集整理的這篇文章主要介紹了
python调用企业微信接口发送群聊消息代码参考
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# Author: sea 2019
import requests
import json
import time
class WebchatUtil:
corpid = '必須填寫你自己申請的'
secret = '固定填寫你自己申請的'
access_token = ''
@staticmethod
def init_access_token():
# 獲取token,必須最長兩個小時換一次7200秒
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}'
getr = requests.get(url=url.format(WebchatUtil.corpid, WebchatUtil.secret))
WebchatUtil.access_token = getr.json().get('access_token')
def sendMarkdownMessage(chatid,*,app_name,ip,err_count,errs_str):
n_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
data = {
"chatid": chatid,
"msgtype": "markdown",
"markdown": {
"content": " # 日志風險預警
## 錯誤主體:<font color="info">{app_name}({ip}) </font>
### 統計時間: <font color="comment"> {n_time}</font>
### 錯誤總數: <font color="warning">{err_count}</font>
### 詳情摘要:
#### {errs_str}
### 每個錯誤都是質量風險,請相應負責人對每個錯誤及時跟進處理好,如需了解更多日志詳情,請登錄服務器查看具體日志文件".format(app_name=app_name, n_time=n_time,ip=ip, err_count=err_count, errs_str=errs_str)
},
"safe": 0
}
m_url = 'https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token={}'
result = requests.post(url=m_url.format(WebchatUtil.access_token), data=json.dumps(data))
return result.json()
@staticmethod
def sendTextMessage(chatid,content):
data = {
"chatid": chatid,
"msgtype": "text",
"text": {
"content": content
},
"safe": 0
}
m_url = 'https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token={}'
result = requests.post(url=m_url.format(WebchatUtil.access_token), data=json.dumps(data))
return result.json()
if __name__ == '__main__':
WebchatUtil.init_access_token()
WebchatUtil.sendMarkdownMessage( 'xxx' , app_name=xxx, ip=xxx,err_count=xxx, errs_str=xxx )
總結
以上是生活随笔為你收集整理的python调用企业微信接口发送群聊消息代码参考的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pymongo bugfix后记
- 下一篇: python面向对象初识