Python implementation WeChat push template Message Function example

Source: Internet
Author: User
This article mainly introduced the Python programming the Push template message function, combined with the instance form analysis Python pushes the message interface the call related operation skill, needs the friend can refer to the next

This example describes the Python push template message feature. Share to everyone for your reference, as follows:

The specific code is as follows:


#!/usr/bin/env python#-*-coding:utf-8-*-import httplibimport jsonimport mysqldb# get access_tokenaccess_token= from the database "" Try:conn=mysqldb.connect (host= ' 192.168.1.1 ', user= ' root ', passwd= ' root ', db= ' db_weixin ', port=3306) cur=conn.cursor () Cur.execute (' Select Access_token from Weixin_public ') Result=cur.fetchone () #print result #print Result[0] Access_ Token=result[0] Cur.close () conn.close () except Mysqldb.error,e:print "Mysql Error%d:%s"% (E.args[0], e.args[1]) #根据接 The port push message if not Access_token is None:conn = Httplib. Httpconnection ("api.weixin.qq.com:80") #接口链接 headers = {"Content-type": "Application/json"} #application/ x-www-form-urlencoded params = ({' Touser ': "Oegz4johnkotayjbneveuazr6zq0", #用户openid ' template_id ': ' Atfuydv8k_ 15ugzufntabzjrcshckjnm1dcwd3a-11y ', #模板消息ID ' url ': ' Http://www.jb51.net ', #跳转链接 "Topcolor": "#667F00", #颜色 "data": {#模板 Content "First": {"value": "Respectable 710.so: your website http://www.jb51.net (192.168.1.1) has unusual access", "Color": "#173177"}, "Keyword1" : {"Value":"Access time 2015-04-05 15:30:59 access IP 192.168.1.2", "Color": "#173177"}, "Keyword2": {"value": "Access link http://www.jb51.net", "Color": "#173177"}, "remark": {"value": "Access Frequency 10/s", "Color": "#173177"}}) Conn.request ("POST", "/cgi-bi N/message/template/send?access_token= "+access_token, JSON. Jsonencoder (). Encode (params), headers) #推送消息请求 response = Conn.getresponse () data = Response.read () #推送返回数据 if RESPONSE.S Tatus = = 200:print ' success ' Print data else:print ' fail ' conn.close ()

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.