Micro letter robot,

Use tools: itchat, requests

Build a python development environment Needless to say this thing, self-Baidu, point to note is that python will remember installation path environment variable to join ~

Installation itchat library

By administrator rights to run the command prompt, enter the following code directly:

pip install itchat

pip install requests

The main program is to log on to the web end of the micro channel by using itchat library, and then to send and receive messages via itchat.

Python micro-channel robot
1. The need to use a module:

requests: Requests are written in Python, based on urllib, using Apache2 Licensed open-source HTTP protocol library. It is more convenient than urllib, we can save a lot of work to fully meet the needs of HTTP test. Requests Philosophy Is PEP idioms 20 centered development.

Itchat: itchat is an open source micro-channel interface to a personal number, using python call micro-channel has never been easier.

Less than thirty lines of code to use, you can complete a robot capable of handling all micro-channel information.

Of course, use the api far more than a robot, more features waiting for you to discover, such as these.

The interface is shared with the public No. interfaces itchatmp similar mode of operation, learning time to master the two tools.

Today, micro-channel has become a significant part of the personal social and hope that this project can help you expand your personal micro-signal, to facilitate their lives

2. Install:

This command can be mounted:

pip install requests
pip install itchat

3. achieved by operation codes

"""
Created on Thu Jun 6 23:17:33 2019

@author: LuDuo
"""

import itchat
import requests

api_key='6d58b43271911efeef94dda0eb68e36a'
api_secret='2ea7dtp0g23y'
def get_response(msg):
apiurl = 'http://i.itpk.cn/api.php' #//moli机器人的网址
data={
"question": msg, #//获取到聊天的文本信息
"api_key": api_key,
"api_secret":api_secret
}

r=requests.post(apiurl,data=data) #//构造网络请求
return r.text
@itchat.msg_register(itchat.content.TEXT) #//好友消息的处理
def print_content(msg):
return get_response(msg['Text'])
@itchat.msg_register([itchat.content.TEXT], isGroupChat=True) #//群消息的处理
def print_content(msg):
return get_response(msg['Text'])
itchat.auto_login(True) #//自动登录
itchat.run() #//启动聊天机器人

哈哈哈以下是通过机器人实现的聊天

如果大家也想被坑的话也快去弄一个茉莉吧

 

Guess you like

Origin www.cnblogs.com/a123456-/p/11005798.html