100 lines of code to achieve public offer robot number background

Foreword

No public use Python Flask background for rapid development, the core code less than 100 lines. Therefore, this open source application is only for the purpose it as a public micro-channel number automatic reply HelloWorldprojects of interest to the students provide a reference.

Micro API Letter

API is a micro-channel micro-channel public platform for users to have the ability to provide development tools and interfaces, developers can refer to the introduction developer documentation micro letter . Develop public number backstage require a public network server, users of public numbers were concerned, when sending messages, sending event and micro-channel background will corresponding to structured data (XML) way onto the public network server we configured , according to the format defined in the document package in response to the event can be customized auto reply.

In addition to the micro-channel message API interface comprises a number of public rear end, further comprising a client JSAPI, we do not relate to the latter.

signature

When ordinary users micro-channel message to the public accounts, micro-letters POST XML data server message packets to fill out the developer's URL. In theory, it can also be directly resolved, but in fact request also with signing request data. On the one hand it is to prevent uploading data being tampered middle, more importantly, on the other hand is to verify the authenticity of sources, ensure that the message is legitimate from the micro-channel server.

For example in a PHP official website, where the look python example:

def verify_request(request):
    signature = request.args.get('signature', '')
    timestamp = request.args.get('timestamp', '')
    nonce = request.args.get('nonce', '')
    echostr = request.args.get('echostr', '')

    data = [MP_TOKEN, timestamp, nonce]
    data.sort()

    calc_sig = hashlib.sha1(''.join(data).encode()).hexdigest()
    if calc_sig == signature:
        return echostr
    else:
        print('Invalid message: signature error!')
    return None

Which MP_TOKENis a token number of public developer background. The above code is very simple, is the token, the timestamp and the random number combination SHA1 hash, and comparing the hash with the request.

automatic response

In the developer documentation received ordinary message section describes the format of the micro-channel server sends a user sends a message presented, exemplary XML request data as follows:

<xml>
  <ToUserName><![CDATA[toUser]]></ToUserName>
  <FromUserName><![CDATA[fromUser]]></FromUserName>
  <CreateTime>1348831860</CreateTime>
  <MsgType><![CDATA[text]]></MsgType>
  <Content><![CDATA[this is a test]]></Content>
  <MsgId>1234567890123456</MsgId>
</xml>

Lxml comes with Python modules required to extract the contents, processes, and constructed to automatically return the corresponding reply. Also the response is in XML format, in response to the following examples:

<xml>
  <ToUserName><![CDATA[toUser]]></ToUserName>
  <FromUserName><![CDATA[fromUser]]></FromUserName>
  <CreateTime>12345678</CreateTime>
  <MsgType><![CDATA[text]]></MsgType>
  <Content><![CDATA[你好]]></Content>
</xml>

The above response message sent to the user's reply text message, "Hello", corresponding to other types of messages, such as images, voice, video, graphic messages, etc., refer to the documentation of the passive user reply message section.

The Code

In the public offer robot number, I sent a message to the user on the electronic currency trading website e-money price, or trading volume corresponding information, and as a text message back to the user via the API requests. It is worth mentioning that not every user request I will be online query, but set a certain window of time during which the user's request simply returns the cached data, which can reduce the pressure on the parties to the server.

The final complete code, see Gitee , need to be modified before running the data/token.txtfile, the content is token micro-channel public platform in the background define yourself. And ultimately the effect is as follows:

demo

Partners can also be of interest to small micro-channel scan code Experience:

qrcode

postscript

Before I test the public number called "quote robot", mainly used to query electronic currency prices. I wanted to create a new account to update the article, but because of the identity of a micro-channel requirements can only sign up for a subscription number, so we can only be occupied. Therefore, the code also publicly quoted robot, easy to need to build their own small partners.

Future public number will be a change of name is not updated regularly on a number of topics for network security. Technical articles and more professional as the case may still be put on the blog, after all, the phone is not suitable as a tool for serious study and research.

Guess you like

Origin www.cnblogs.com/pannengzhi/p/opensource-mp-bot.html