使用itchat实现一个微信机器人聊天回复功能

最近看到好多群里都有一个@机器人的功能,挺有趣的,想自己也玩下,就通过百度一点点实现,在这总结一下整个从无到有的过程。

首先,要知道itchat,它是Python写的,所以想要实现这个机器人的功能,需要使用Python(就是Python,ε=(´ο`*)))唉),我是一点Python都不会,但是没关系,整个编码也就只需要几行,网上也有很多实现,可以参考。

1、安装Python环境

我的电脑是从来都没有搭建过Python相关的东西,所以想要运行,就需要先安装Python环境。

从官网https://www.python.org/downloads/ 上下载最新的Python安装包,然后安装,一定要知道被安装到哪个目录了

2、添加环境变量

先找到刚刚安装的Python,Python.exe在哪个目录下

如果找不到,可以试试找到下面这个,然后右键属性,看看它的目录(d:\Users\hello\AppData\Local\Programs\Python\Python37-32\python.exe)

选中path然后编辑,注意环境变量的每个值之间使用分号隔开的,所以最后一个值如果没有分号,要手动加一个,然后把刚刚找到的目录加到最后面(注意要去掉Python.exe),所以追加的值就是d:\Users\hello\AppData\Local\Programs\Python\Python37-32

 保存完之后,打开命令行,输入Python,显示下面的信息,说明Python,已经安装成功了

3、安装pip

pip 是一个现代的,通用的 Python 包管理工具。提供了对 Python 包的查找、下载、安装、卸载的功能。

下载路径:https://pypi.python.org/pypi/pip#downloads

下载后,解压,然后找到这个目录,在这个目录下,打开命令行窗口,执行 

python setup.py install

同样的方式,将D:\Users\hello\AppData\Local\Programs\Python\Python37-32\Scripts添加到Path 中,然后在命令行执行

pip list

此时Python的环境就全部安装完成了。

4、安装itchat

在命令行执行

pip install itchat

5、编写python文件

我使用的IDE是vscode,因为没有开发过Python,所以需要在扩展中搜索Python,并且安装

安装完成后,新建一个test1.py

直接拷贝下面的代码,到test1.py 中,然后调试运行,会弹出一张二维码,用手机微信扫描后,登录(此时你的账号,登录的就是电脑网页版的微信,所以电脑上这个时候不要登录微信,如果电脑上已经登录了微信,那么这个时候电脑上的微信会提示你,你的账号已在其他地方登录)

import itchat

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
    return msg.text

itchat.auto_login()
itchat.run()

在手机端的微信,这个时候你的好友给你发的任何信息,都会自动返回相同的信息给你的好友。现在就实现了自动回复的功能。

6、调用微软小冰接口,实现聊天回复

在命令行执行,安装 requests 和 urllib

pip install requests
pip install urllib3 //3是urllib的版本号

拷贝下面的代码,运行

# 加载包
import itchat
import requests
import urllib.parse

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
   robotChat(msg)

def robotChat(msg):
    print('robot chat')
    print(msg['Text'])
    sendMsg = msg['Text'].strip()
    try:
        r = requests.get('https://www4.bing.com/socialagent/chat?q=' + sendMsg+'&anid=123456')#小冰接口
        try:
            r1= r.json()
            info = urllib.parse.unquote(r1['InstantMessage']['ReplyText'])
            print(info)
            itchat.send(info, toUserName = msg['FromUserName'])#发送回复消息
        except Exception as e2:
            print(e2)
    except Exception as e:
        print(e)

itchat.auto_login()
itchat.run()

7、群聊,@回复

# 加载包
import itchat
import requests
import urllib.parse


@itchat.msg_register(itchat.content.TEXT, isGroupChat = True)
def group_reply(msg):
    if msg['isAt']:
        robotChat(msg)


def robotChat(msg):
    print('robot chat')
    print(msg['Text'])
    sendMsg = msg['Text'].strip()
    if sendMsg.find('@'+msg['User']['Self']['DisplayName'])==0:
        sendMsg = msg['Text'][len(msg['User']['Self']['DisplayName']) + 2:]
    elif msg['Text'].find('@'+msg['User']['Self']['DisplayName']) > -1:
        sendMsg = msg['Text'].replace('@'+msg['User']['Self']['DisplayName'], '')
    print(sendMsg)
    try:
        r = requests.get('https://www4.bing.com/socialagent/chat?q=' + sendMsg+'&anid=123456')
        try:
            r1= r.json()
            info = urllib.parse.unquote(r1['InstantMessage']['ReplyText'])
            print(info)
            itchat.send(info, toUserName = msg['FromUserName'])
        except Exception as e2:
            print(e2)
    except Exception as e:
        print(e)

以上就是itchat最简单的文本回复功能

猜你喜欢

转载自www.cnblogs.com/panyujun/p/9376810.html