1.通知方法は何ですか?
一般的な通知方法は、電子メール、電話、SMS、WeChatです。
SMSと電話:通常は有料でほとんど使用されません;
電子メール:ファイルタイプの通知に適しており、より正式で、アーカイブに使用されます;
WeChat:アラームタイプの通知に適しており、より便利です。ここで言及されているWeChatはエンタープライズWeChatです。
この記事の目的は、企業のWeChatアプリケーションを介して企業のメンバーにメッセージを送信することです。
2.エンタープライズWeChat通知を実現するにはどうすればよいですか?
1.新しいアプリケーションを作成する
WebバージョンのEnterpriseWeChat(https://work.weixin.qq.com)にログインし、[アプリケーション管理]→[アプリケーション]→[アプリケーションの作成]をクリックしてアプリケーション
のロゴをアップロードし、アプリケーション名を入力します(新しいボンド)、表示範囲を選択し、アラームアプリケーションを正常に作成しました
。2。シークレットを取得し、
Pythonを使用してアラームリクエストを送信します。実際には、次の2つのインターフェイスのみが使用されます。
トークンを取得:https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid = {corpid}&corpsecret = {secret}
リクエストの送信:https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token = {token}
ご覧のとおり、最も重要なのは、心のこもった秘密です。
Corpid:ビジネスを一意に識別します
シークレット:アプリケーションレベルのキー。プログラムは、送信する企業のアプリケーションを認識します。
Corpidは、MyCompany→CompanyInformation→CompanyIDから取得できます。
新しく作成したアプリケーション(新しいボンド)→View secret→Sendをクリックすると、シークレットを取得できます。
最後に、次の定数にコーピッドとシークレットを入力します。
3.コードの実装
import json
import time
import requests
'''
本文件主要实现通过企业微信应用给企业成员发消息
'''
CORP_ID = "xxxx"
SECRET = "xxxx"
class WeChatPub:
s = requests.session()
def __init__(self):
self.token = self.get_token()
def get_token(self):
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={
CORP_ID}&corpsecret={
SECRET}"
rep = self.s.get(url)
if rep.status_code != 200:
print("request failed.")
return
return json.loads(rep.content)['access_token']
def send_msg(self, content):
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + self.token
header = {
"Content-Type": "application/json"
}
form_data = {
"touser": "FengXianMei",#接收人
"toparty": "1",#接收部门
"totag": " TagID1 | TagID2 ",#通讯录标签id
"msgtype": "textcard",
"agentid": 1000002,#应用ID
"textcard": {
"title": "债券打新提醒",
"description": content,
"url": "URL",
"btntxt": "更多"
},
"safe": 0
}
rep = self.s.post(url, data=json.dumps(form_data).encode('utf-8'), headers=header)
if rep.status_code != 200:
print("request failed.")
return
return json.loads(rep.content)
if __name__ == "__main__":
wechat = WeChatPub()
timenow = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
wechat.send_msg(f"<div class=\"gray\">{
timenow}</div> <div class=\"normal\">注意!</div><div class=\"highlight\">今日有新债,坚持打新!</div>")
print('消息已发送!')
4.効果を達成します。
3.注意が必要な事項
4.参考文献
PythonでWeChatにアラート通知を送信するにはどうすればよいですか?
Pythonを使用してWeChatパブリックアカウントにメッセージを送信します