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

Implementing wechat robot with Python -- itchat Library

編輯:Python

itchat Is an open source wechat personal number interface , You can use the library to do all the operations in wechat web page version , such as : All friends 、 Add buddy 、 Group chat with friends 、 Wechat robot and so on . For details, please refer to the document , ad locum .

This paper mainly uses the library to complete a Turing machine that can process wechat messages , Including friends chat 、 Group chat .

1、itchat The installation of the library

pip install itchat

After the installation is complete, run the following code , A QR code will appear , After scanning and logging in, you will log in to wechat website .

2、 land

import itchat
# land
itchat.auto_login() # Can be set up hotReload = True
# Run and stay online
itchat.run()

Besides ,itchat It also provides the function of disconnection and reconnection in a short time , Just add hotReload = True Parameters , The next time you log in, you don't need to scan the QR code , Just confirm the login on the mobile phone .

3、 Sending of messages

itchat Libraries can send Text 、 picture 、 video 、 Attachments, etc , For example, send a message to wechat file transfer assistant , But this is it :

itchat.send('Hello', toUserName='filehelper')

toUserName For a micro signal to the person who sent the message , You can click query on wechat mobile terminal , You can also use itchat In the library search_friends Function to find , It returns its micro signal , Detailed usage , Find your own official documents .

4、 Reception of messages

###################### Complete code ##############################
# Load the library
from itchat.content import *
import requests
import json
import itchat
itchat.auto_login(hotReload = True)
# Call the Turing robot api, Using the principle of reptiles , Return the reply content according to the chat message
def tuling(info):
appkey = "e5ccc9c7c8834ec3b08940e290ff1559"
url = "http://www.tuling123.com/openapi/api?key=%s&info=%s"%(appkey,info)
req = requests.get(url)
content = req.text
data = json.loads(content)
answer = data['text']
return answer
# For information group chat , Define the group to get the robot reply for a group ID function
def group_id(name):
df = itchat.search_chatrooms(name=name)
return df[0]['UserName']
# Register text messages , Bound to the text_reply Processing function
# text_reply msg_files Can handle the chat reply between friends
@itchat.msg_register([TEXT,MAP,CARD,NOTE,SHARING])
def text_reply(msg):
itchat.send('%s' % tuling(msg['Text']),msg['FromUserName'])
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
msg['Text'](msg['FileName'])
return '@%[email protected]%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName'])
# Now wechat has added many groups , I don't want to set up wechat robots for all groups , Wechat robot only for the group you want to set up , The following settings can be made
@itchat.msg_register(TEXT, isGroupChat=True)
def group_text_reply(msg):
# Of course, if you just want to target @ Your talent reply , You can set if msg['isAt']:
item = group_id(u' The name of the group you want to set ') # Set up according to your own needs
if msg['ToUserName'] == item:
itchat.send(u'%s' % tuling(msg['Text']), item)
itchat.run()

that , Here's your friend 、 Wechat group can chat with Turing robot happily ~~~

If you find something wrong or don't understand , In the comments section , Let's talk !


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