Python3 calls the WeChat enterprise account API to send a text message sample code,

Source: Internet
Author: User

Python3 calls the enterprise id api to send a text message sample code,

This article mainly shares with you the code of Python3 calling the enterprise number API to send text message examples, as follows:

#!/usr/bin/env python# -*- coding:utf-8 -*-import urllib.requestimport jsonimport sysimport loggingtouser = '@all'agentid = 0corpid = '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)

Summary

The above is all the content in this article about Python3 calling the enterprise code API to send text message code samples. I hope it will be helpful to you. If you are interested, you can continue to refer to this site: Share the verification code Image Code in Python web, implement the facial recognition code in python, and crawl funny website jokes from Python crawler instances, if you have any questions, you can leave a message at any time. The editor will reply to you in a timely manner. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.