No public small game developers to share practical operation of fission

Inspiration

Yesterday circle of friends to see such a Share:

Here Insert Picture Description

After curiosity drove me to sweep the two-dimensional code (a code has been) the figure found is to guide public concern number of concerns me a little, came out of this session interface predict 2020 will take place at the scan code of a few persons who thing game link:

Here Insert Picture Description

I continue to click on this link, and then generates a prediction map of my own:

Here Insert Picture Description

Then I was able to share it, so my friends circle of friends to sweep, they share, such a mass ten, a hundred, unknowingly been separated for a wave of leeks:

Here Insert Picture Description

Creative effects

As a technical person, not only a keen sense of smell to cut leeks, but also know how technology is cutting leeks, Ever since last night, a night investigation to determine the technology stack and with the understanding of many restrictions on the development of public numbers, and shift today one morning, I took advantage of my micro letter public trumpet, deploy the application on-line.

Sweep can be swept away in the following two-dimensional code ~ experience, independently one time is limited, do not spray the UI ugly.

Here Insert Picture Description

(Ps ~, not followers clicks attention, has sent posters Followers word, you can get your exclusive predict posters, poor server performance, optimized code is yet to be larger picture transfer traffic, it takes 1-3 seconds to get poster you made, please understand)

Creation strokes

Two development models micro-channel public number

You may have a doubt? Why do I use small test? In fact, the development of micro-channel public number two modes:

Edit Mode: mainly used for non-programmers and information dissemination section of the public accounts. After opening this mode, can easily configure "Custom Menu" and "auto-reply message" through the interface.

Development model: The main use for people with the ability to develop. After opening this mode, it is possible to use the micro-channel open to the public internet interface, create a custom menu programmatically, the user message received / treatment / response. This model is more flexible, the proposed development capabilities of companies or individuals using this mode.

Simply put, without writing code directly configure a custom menu on the micro-channel public number of pages or auto-reply, called the edit mode; write code to achieve automatic reply or more advanced logic, called development model; and these two modes are mutually exclusive, that is, once opened a development mode, edit mode becomes ineffective, previously configured custom menus and auto-reply all the failures, it is unacceptable to me now that the public number, because too configuration in edit mode more, developer mode to migrate to a more consumption of time cost, and secondly, is not stable, so I thought of my dusty trumpet.

Writing materials

Before the formal development, you need to have the "paper" or "pen"

  1. One server (the best location in South Central North East West)

  2. One of the best record good domain name

Public access server micro channel number

And then, in fact, micro-channel public number during development, micro-channel public number backstage is like a proxy server, micro-channel public number of the message first by the micro-channel public number background processing, forwarded to our own server processing, this process must be follow the development of norms of the micro-channel public number; so, how to let the public micro-channel number and docking it on our own server? Public internet access micro-channel development, developers need to complete the following steps:

Fill in the server configuration: Enter URL (only support 80 (http) and (https) 443 port, developer documentation says that the domain name server, it is not exactly your domain name server, but you deal with the root micro-channel public number forwarded messages on the server routing), the Token (can easily fill) and EncodingAESKey (automatic generation), these two parameters can only be useful when the access is for security reasons and uses two encryption sha of several parameters without undue concern, this step is mainly pay attention to the URL is correct.

Verify the validity of the server address: Tap verification button, Token verify the success or failure prompt will appear on the page

Based interface documentation to implement business logic: this is our mettle to write code part.

Application logic

There is a need to fill out the pit, personal micro-channel public number is not able to obtain basic information about the user: nicknames and avatars etc.

Here Insert Picture Description

Authentication is required to get the permission, but certification is required take money, but the poor are written on my face

Returning to our creative mind, involving personalized prediction posters, personalized section I will briefly deal with direct random string, then dig deep if need personalized recommendations, but there is no user information, do not allow users to write several logical lose yourself hobby? It has been out of the original intention of this writing, the next time to make up.

As for the production of posters, ImageDraw objects PIL library I used in, the string can be directly filled in on default poster.

PIL library installation posture is pip install pillow instead pip install PIL

The application server then uses the flask frame, we assume that the root routing https: // doamin / wechat /
we will give a view of the route binding function, the my_view (), while post processing request switch is turned on (by default only get processed ), all of our logic can be implemented by this view function, Token verification number is public request to our server get by way of the access number of public development, forwarding the message after the verification by the way is by post, then, we can distinguish between verified by way of a request or forwarded.

if request.method == 'GET':
	signature = request.args['signature']
	timestamp = request.args['timestamp']
	echostr = request.args['echostr']
	nonce = request.args['nonce']
	token = '好想爱这个世界'
	tempList = [token, timestamp, nonce]
	# 字典序排序
	tempList.sort()
	# 转成字符串
	tempStr = ''.join(tempList)
	# sha1 加密
	tempStr = hashlib.sha1(tempStr.encode('utf-8')).hexdigest()
	if tempStr == signature:
	print('token verify succes')
		return echostr
	else:
	#可以发现,前面的验证代码其实无关紧要,只要返回了 echostr ,就算验证成功
		return echostr
		
elif request.method == 'POST':
	print('在这里写业务逻辑')

We also need to call micro letter on your own server interface, such as the poster finished, forwarded through the micro-channel to the user, can not be directly returned pictures of binary data, in fact, this is the micro-channel public number do, we need to upload the poster to the micro-letter No. public library, id get this poster, and then packaged into xml data back to the public micro-channel number (Tucao about json have not upgraded to the exchange of data), so users receive our poster.

So the question is, upload the poster to the micro-channel public number Library, in public No. article editing interface spent a long time, I thought to manually upload, but fortunately found the interface to 1, we can call this interface 1 on the server, in order to 1 to prevent abuse of this interface, you must provide access_token, but where access_token to do? 2 required by calling another interface, the interface needs to provide AppId 2 and AppSecret parameters, if not their own modifications, both long-term and effective, can be obtained directly.

Here Insert Picture Description

Interface 1: POST https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE

Interface 2: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

There is a pit, the access_token only valid for two hours, so we need to constantly update it, so it is necessary for us to introduce the flask cycle task configuration

# 定时任务配置
class Config(object):  # 创建配置
	# 任务列表
	JOBS = [
		{
			'id': '1',
			'func': '__main__:refreshToken',  # 定时执行的方法名
			'trigger': 'interval',  # interval表示循环任务
			'hours': 2, # 每两个小时的执行一次
		}
	]
app.config.from_object(Config())  # 为实例化的flask引入配置

Pit being filled so much, welcome to the big Karma tasting. Finally, to prevent the entrance poster we forget, add a map, only production-level deployment environment, do not accept any pressure test, you need to offer to buy servers, please leave a comment below disturb.

Here Insert Picture Description

Published 84 original articles · won praise 250 · Views 150,000 +

Guess you like

Origin blog.csdn.net/ygdxt/article/details/103429393