加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python3调用微信企业号API发送文本消息代码示例

发布时间:2020-12-17 07:55:23 所属栏目:Python 来源:网络整理
导读:本文主要向大家分享了Python3调用微信企业号API发送文本消息示例的有关代码,具体如下: #!/usr/bin/env python# -*- coding:utf-8 -*-import urllib.requestimport jsonimport sysimport loggingtouser = '@all'agentid = 0corpid = 'wx5aef2da956514535'co

本文主要向大家分享了Python3调用微信企业号API发送文本消息示例的有关代码,具体如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib.request
import json
import sys
import logging
touser = '@all'
agentid = 0
corpid = 'wx5aef2da956514535'
corpsecret = 'Co17m_OPlvE8Q4P2RKKwtq5oIA3p42xGUZEvCHBI8S0'
url = 'https://qyapi.weixin.qq.com'
subject = sys.argv[2]
message = sys.argv[3]
logging.basicConfig(level=logging.DEBUG,filename='E:Python_projectScriptsmy.log',format='%(asctime)s - %(levelname)s: %(message)s')
class Weixin:
  def __init__(self,url,corpid,corpsecret):
    token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url,corpsecret)
    self.token = json.loads(urllib.request.urlopen(token_url).read().decode())['access_token']
  def send_message(self,data):
    send_url = '%s/cgi-bin/message/send?access_token=%s' % (url,self.token)
    self.respone = urllib.request.urlopen(urllib.request.Request(url=send_url,data=data)).read()
    x = json.loads(self.respone.decode())['errcode']
    if x == 0:
      logging.debug('Successfully %s  %s' % (subject,message))
      return 'Succesfully'
    else:
      logging.debug('Failed %s  %s' % (subject,message))
      return 'Failed'
  def messages(self,subject,message):
    values = {
      "touser": touser,"msgtype": 'text',"agentid": agentid,"text": {'content': subject + message},"safe": 0
    }
    return self.send_message(url,bytes(json.dumps(values),'utf-8'))
if __name__ == '__main__':
  obj = Weixin(url,corpsecret)
  ret = obj.messages(subject,message)

总结

以上就是本文关于Python3调用微信企业号API发送文本消息代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:在Python web中实现验证码图片代码分享、python实现人脸识别代码、Python爬虫实例爬取网站搞笑段子等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读