preface
A few days ago, I saw an article on the Internet called “Teach you to say good night to your girlfriend every day by wechat”. I felt it was very magical. Then I studied it. Okay, let’s get started! The server is there, the Python environment is there, the IDE is open… However… However… I realized a very serious problem… No girlfriend (T_T)…
WeChat development has been active for a long time, have a magic in the WeChat development interface called template message interface, it can be based on user openid from the server to the user to push a custom template, because of this, we can use this feature on the server side, from time to time, send to users to push messages (premise is that the user pay attention to the public).
Three points are summarized: 1. The format of template messages can be customized; 2. The content of template messages can be customized; 3. The time at which template messages are sent can be customized. So we can use these properties to make a good morning app for ourselves!
Experimental environment
- Aliyun Linux server
- Python 2.7
Iciba daily one API introduction
Call the address: http://open.iciba.com/dsapi/ request: GET request parameters:
parameter | Will choose | type | instructions |
---|---|---|---|
date | no | string | Format for:2013-05-06 ; ifdate If yes, the default value is the current day |
type | no | string | Optional value islast andnext ; In order todate Subject to the date,last Go back to the day before,next Return the day after |
Example request:
The Python example
#! /usr/bin/python #coding=utf-8 import json import urllib2 def get_iciba(): url = 'http://open.iciba.com/dsapi/' request = urllib2.Request(url) response = urllib2.urlopen(request) json_data = response.read() data = json.loads(json_data) return data print get_iciba()
Copy the code
The PHP sample
<? php function https_request($url, $data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); if (! empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } function get_iciba(){ $url = 'http://open.iciba.com/dsapi/' $result = https_request($url); $data = json_decode($result); return $data; } echo get_iciba();
Copy the code
Return type: JSON
The property name | Attribute value type | instructions |
---|---|---|
sid | string | ID of the Day |
tts | string | Audio address |
content | string | English content |
note | string | Chinese content |
love | string | One sentence a day like number |
translation | string | Here small make up |
picture | string | Picture address |
picture2 | string | Big picture address |
caption | string | The title |
dateline | string | time |
s_pv | string | Browse the number |
sp_pv | string | Voice review views |
tags | string | Relevant tags |
fenxiang_img | string | Synthetic picture, suggested to share the micro blog with |
An example is returned normally:
{ "sid": "3080", "tts": "http://news.iciba.com/admin/tts/2018-08-01-day.mp3", "content": "No matter how hard we try to be mature, we will always be a kid when we all get hurt and cry. ", "note": "No matter how hard we try to grow up, when we get hurt and cry, we're still like children." Peter Pan, Peter Pan, Peter Pan, Peter Pan, Peter Pan Time is always young, we slowly old. No matter how you change, you will finally find: retaining a childish heart, is a matter of pride. Sometimes it's easy to grow up, but it's not always easy to enjoy everything with a childlike innocence." , "picture": "http://cdn.iciba.com/news/word/20180801.jpg", "picture2": "Http://cdn.iciba.com/news/word/big_20180801b.jpg", "the caption" : "iciba a daily", "dateline" : "2018-08-01", "s_pv" : "0", "sp_pv": "0", "tags": [ { "id": null, "name": null } ], "fenxiang_img": "http://cdn.iciba.com/web/news/longweibo/imag/2018-08-01.jpg" }
Copy the code
This interface (daily sentence) official document: open.iciba.com/?c=wiki reference: Kingsoft Powerword · development platform
Log in to the wechat public platform interface test account
Scan login public platform test number
Application test, the address of the https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
Confirm login on your phone
findAdding a Test Template
To add template messages
Fill in the following sentence in the template title
{{content.DATA}}
{{note.DATA}}
{{translation.DATA}}
Copy the code
After committing to save, remember thisTemplate ID
I’ll use it in a moment
findTest Number information
Remember,appid
andappsecret
I’ll use it in a moment
findQr code of Test Number
. Mobile phone scan this TWO-DIMENSIONAL code, after attention, your nickname will appear in the right list, remember the micro signal, will be used in a moment (note: this micro signal is not your real micro signal)
Send wechat template message program
This procedure you only need to modify 4 places, please see the notes
#! /usr/bin/python #coding=utf-8 import json import urllib2 class iciba: # def __init__(self, wechat_config): self.appid = wechat_config['appid'] self.appsecret = wechat_config['appsecret'] self.template_id = Def get_access_token(self, appid, appsecret): wechat_config['template_id'] self.access_token =" url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (appid, appsecret) request = urllib2.Request(url) response = urllib2.urlopen(request) json_data = response.read() data = json.loads(json_data) access_token = data['access_token'] self.access_token = access_token return self.access_token # Def send_msg(self, openid, template_id, iciba_everyday): MSG = {'touser': openid, 'template_id': template_id, 'url': iciba_everyday['fenxiang_img'], 'data': { 'content': { 'value': iciba_everyday['content'], 'color': '#0000CD' }, 'note': { 'value': iciba_everyday['note'], }, 'translation': { 'value': iciba_everyday['translation'], } } } json_data = json.dumps(msg) if self.access_token == '': self.get_access_token(self.appid, self.appsecret) access_token = self.access_token url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s' % str(access_token) request = urllib2.Request(url, Data =json_data) response = urllib2.urlopen(request) result = response.read() return json get_iciba_everyday(self): url = 'http://open.iciba.com/dsapi/' request = urllib2.Request(url) response = urllib2.urlopen(request) json_data = Response.read () data = json.loads(json_data) return data # def send_everyday_words(self, openids): everyday_words = self.get_iciba_everyday() for openid in openids: result = self.send_msg(openid, self.template_id, everyday_words) if result['errcode'] == 0: print ' [INFO] send to %s is success' % openid else: Print '[ERROR] send to %s is ERROR' % openid # def run(self, openids) Self. send_everyday_words(openids) if __name__ == '__main__': 'XXXXXX ', # fill in your appID 'appsecret':' XXXXXXX ', # fill in your Appsecret 'template_id': Openids = [' XXXXX ', # fill in your wechat ID #' XXXX ', # if there are multiple users can also be #' XXXX ', Icb = iciba(wechat_config) icb.run(openids)
Copy the code
The test program
Execute the program on Linux
View on the phone, has received a daily message
The deployment process
Set a scheduled task on Linux
crontab -e
Copy the code
Add the following
0 6 * * * python /root/python/iciba/main-v1.0.py
Copy the code
Note: At 6:00 p.m. each day, execute this Python program to see if the scheduled task has been set successfully
crontab -l
Copy the code
At this point, the program deployment is complete, please tomorrow6 PM
Check!!!!
The renderings are as follows