脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python应用之如何使用Python发送通知到微信

python应用之如何使用Python发送通知到微信

2022-11-14 10:46shammy Python

现在通过发微信信息来做消息通知和告警已经很普遍了,下面这篇文章主要给大家介绍了关于python应用之如何使用Python发送通知到微信的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

一、通知方式有哪些?

常见的通知方式有:邮件,电话,短信,微信。

短信和电话:通常是收费的,较少使用;

邮件:适合带文件类型的通知,较正式,存档使用;

微信:适合告警类型通知,较方便。这里说的微信,是企业微信。

本文目的:通过企业微信应用给企业成员发消息。

二、如何实现企业微信通知?

1、新建应用

登陆网页版企业微信 (https://work.weixin.qq.com),点击 应用管理 → 应用 → 创建应用

python应用之如何使用Python发送通知到微信

上传应用的 logo,输入应用名称(债券打新),再选择可见范围,成功创建一个告警应用

python应用之如何使用Python发送通知到微信

2、获取Secret

使用 Python 发送告警请求,其实就只使用到两个接口:

获取 Token :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 和 secret:

corpid:唯一标识你的企业

secret:应用级的密钥,有了它程序才知道你要发送该企业的哪个应用

corpid 可以通过 我的企业 → 企业信息 → 企业id 获取

python应用之如何使用Python发送通知到微信

secret 可以通过 点击 新创建的应用(债券打新) → 查看 secret → 发送 来获取

python应用之如何使用Python发送通知到微信

python应用之如何使用Python发送通知到微信

最后将 corpid 和 secret 填入下面的常量中。

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、实现效果:

python应用之如何使用Python发送通知到微信

python应用之如何使用Python发送通知到微信

python应用之如何使用Python发送通知到微信

三、参考资料

如何用 Python 发送告警通知到微信?

用python给微信公众号发消息

总结

到此这篇关于python应用之如何使用Python发送通知到微信的文章就介绍到这了,更多相关Python发送通知到微信内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/shammy_feng/article/details/123711347

延伸 · 阅读

精彩推荐