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

yipeiwu_com5年前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爬虫实例爬取网站搞笑段子等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

相关文章

wxPython的安装与使用教程

wxPython的安装与使用教程

一、wxPython介绍     1.wxPython是Python语言的一套优秀的GUI图形库。wxPython可以很方便的创建完整的、功能键全的GUI用...

Python3 文章标题关键字提取的例子

Python3 文章标题关键字提取的例子

思路: 1.读取所有文章标题; 2.用“结巴分词”的工具包进行文章标题的词语分割; 3.用“sklearn”的工具包计算Tf-idf(词频-逆文档率); 4.得到满足关键词权重阈值的词...

python数据归一化及三种方法详解

python数据归一化及三种方法详解

数据标准化(归一化)处理是数据挖掘的一项基础工作,不同评价指标往往具有不同的量纲和量纲单位,这样的情况会影响到数据分析的结果,为了消除指标之间的量纲影响,需要进行数据标准化处理,以解决数...

win10系统下python3安装及pip换源和使用教程

win10系统下python3安装及pip换源和使用教程

一、python3的安装 建议安装python3,python2在未来将不再维护。 python官方下载地址 https://www.python.org/downloads/windo...

python生成ppt的方法

python生成ppt的方法

本文主要介绍如何通过python生成ppt文件,以及借助ppt模板来生成ppt 环境 python 3 python-pptx 安装 pip3 install python-p...