程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Wrote a wechat chat robot in Python

編輯:Python

In our life and work , Many times we can't reply to the news in time , Especially people with more business , The customer sends us a message. It's not good if we don't return it , But I don't have so much energy to reply from time to time , At this time, intelligent robots can help us solve many problems .

Customer service like e-commerce , Like big QQ Group / Wechat group administrator , And when we play games , Just hang the script , The robot will automatically help you return messages , I won't offend my girlfriend for a while , Ha ha ha !

Let's teach you a move today ,16 That's ok Python Code implementation 1 A wechat chat intelligent robot ( Turing ), Don't worry about being scolded by customers for not returning messages !

One 、 effect

I take my own wechat ( On the left ) After landing , I tested it with my assistant's wechat , The chat effect looks good after the robot took over my wechat :

 

Two 、 Project ideas

The overall idea is shown in the figure below :

3、 ... and 、 The code analysis

1. Install and import modules

Here are two ,itchat Module and requests modular , This is an essential step , The project runs based on these two modules . The installation method is very simple , Direct window key +R Call up the command window , Then enter the instructions to install , For example, installation itchat The module directly inputs pip install itchat.

Hint , The two modules are installed separately , Finish loading 1 One for the other .

After installation , We can import modules directly into the code :

import itchat
import requests

2. Apply for Turing robot

The robot of this project is a ready-made Turing robot , We need to register on Turing's official website , Then enter the background to create the robot , Every robot has 1 individual api, This is what we need .

Before Turing robot, there were free version and experience version , Now you need real name authentication to have , Although only 100 The second message reply , But it's OK for testers to play .

Turing can also reply to the personality of keywords , Backstage “ Private corpus ” Just set it up .

3. Realize robot function

In addition to visiting the website url outside , Also determine which robot is calling , Because maybe you have multiple robots backstage , At this time, the unique of each robot is used api, And get the message to send , And then for post Send request by , Send website url、 Robotic api And the message to send , Finally, extract... From the dictionary text, Finally, return data .​​​​​​​

4. Realize wechat function

If someone sends us a message , We call the data returned above , So how do you know if someone sent you a message ? At this time, we used decorators ,itchat Provided @itchat.msg_register() You can do this ; Then call the robot function written above , Finally return the information of the robot or the sender .

Why add the sender's information, copy it and send it again ? This is to prevent other problems such as the network from causing the robot not to respond , Who sends us any news at this time , We will automatically reply to him with the same message , Avoid being unresponsive while chatting .

@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(user_data):
print(user_data)# User information includes messages sent to you
user=user_data["Text"]# Users send your message
return_user = get_response(user_data["Text"])
print(return_user)
return return_user or user

5. Pop up QR code and loop program

When the program is running, we need to pop up 1 QR codes , Let's scan the code and log in , At the same time, ensure that the program is always running , Otherwise, the program will only run once , These two functions are itchat provide , Just use it directly .​​​​​​​

itchat.auto_login()# Sweep the login code
itchat.run()# Loop execution 

The above is the code analysis of this wechat intelligent chat robot , This dummy is still good , I don't know the paid version , Charging is sure to be stronger , No more than 20 Line code .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved