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

yipeiwu_com6年前Python基础

本文主要向大家分享了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_project\Scripts\my.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, corpid, corpsecret)
    self.token = json.loads(urllib.request.urlopen(token_url).read().decode())['access_token']
  def send_message(self, url, 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, corpid, corpsecret)
  ret = obj.messages(subject, message)

总结

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

相关文章

Python 实现毫秒级淘宝抢购脚本的示例代码

本篇文章主要介绍了Python 通过selenium实现毫秒级自动抢购的示例代码,通过扫码登录即可自动完成一系列操作,抢购时间精确至毫秒,可抢加购物车等待时间结算的,也可以抢聚划算的商品...

Python的Flask框架中配置多个子域名的方法讲解

Flask子域名 一般用于数量比较少的子域名,一个模块对应一个子域名。先看下面一个例子: modules.py: from flask import Blueprint publi...

在python中实现将一张图片剪切成四份的方法

如下所示: import cv2 # [1]导入OpenCv开源库 import numpy as np image_path = "F:\\111111111111111111...

python3射线法判断点是否在多边形内

本文实例为大家分享了python3射线法判断点是否在多边形内的具体代码,供大家参考,具体内容如下 #!/usr/bin/python3.4 # -*- coding:utf-8 -*...

浅析Python3 pip换源问题

pip安装源 背景# 在实际开发中, 可能要大量使用第三方模块(包), 更换至国内下载源, 可大幅提升下载速度 """ 1、采用国内源,加速下载模块的速度 2、常用pip源:...