使用itchat和图灵机器人实现微信机器人

1.首先先安装python的itchat库

pip install itchat

2.在图灵机器人官网注册一个apikey

3.编写机器人代码

import itchat
from itchat.content import *
import itchat
import json,re
import requests

itchat.auto_login(hotReload = True) #
#从图灵机器人API接入接口 def tuling(info): #appkey = "e5ccc9c7c8834ec3b08940e290ff1559" appkey = '***********'#你的图灵apikey,图灵机器人秘钥 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 # 获得群聊id def get_group_id(group_name): group_list = itchat.search_chatrooms(name=group_name) return group_list[0]['UserName'] # 自动通过加好友 add_friend_compile = re.compile(r'.*?') @itchat.msg_register(itchat.content.FRIENDS) def deal_with_friend(msg): if add_friend_compile.search(msg['Content']) is not None: itchat.add_friend(**msg['Text']) # 自动将新好友的消息录入,不需要重载通讯录 itchat.send_msg('嘤嘤嘤,我是智障机器人Coder,\n很高兴认识你,回复关键字:\n\n 加群:加入社团名称\n公众号:获取包含超多实用校园服务的创软公众号 \n\n 来吧!走进我们的代码世界~', msg['RecommendInfo']['UserName'])
#当群里有新人加群的时候,自动发送欢迎词 @itchat.msg_register(NOTE,isFriendChat=True,isGroupChat=True,isMpChat=True) def wellcome(msg): item = get_group_id(u'测试群') #可以改为你自己的群聊名字,首先要把群聊加入通讯录 chatroom = itchat.update_chatroom(item) # print(chatroom['MemberList']) if '加入了群聊' in msg['Content']: itchat.send("@"+chatroom['MemberList'][-1]['NickName']+"\n欢迎新同学!大家关于社团或是学校有什么问题都可以在群上直接问哦!我们会尽力解答的。\n进群都改下备注哦~\n“年级_专业_姓名”\n例如:17_软件技术_狗蛋 \n如果想报名我们社团,可以关注创软公众号回复“加入”", item) # print(chatroom['MemberList'][-1]['NickName']) @itchat.msg_register(TEXT, isGroupChat=True,) def group_text_reply(msg): # 在群聊中只对@你的人才回复,可以设置if msg['isAt']: if msg['isAt']: itchat.send(u'%s' % tuling(msg['Text']), msg['FromUserName'])
#加了微信机器人好友后,发送关键词,获得进群消息 @itchat.msg_register([TEXT]) def deal_with_msg(msg): text = msg['Content'] if text == u'加群': # itchat.add_member_into_chatroom(get_group_id(u"测试群"), [{'UserName': msg['FromUserName']}]) # itchat.send(str(itchat.search_friends(userName=msg['FromUserName'])['NickName']),toUserName='filehelper') itchat.send_msg(str(itchat.search_friends(userName=msg['FromUserName'])['NickName']), toUserName='filehelper') itchat.send_msg('您的加群信息已收到\n稍后(我也不知道多久)将会拉您进群', msg['FromUserName']) itchat.send_image('timg.gif', msg['FromUserName']) #发送一个骚气表情包
     #机器人收到加群消息后,会向文件接收助手发送好友昵称,由于微信网页的自动拉人功能被封了,所以只能通过手动发送进群消息
elif text == u'公众号': itchat.send_msg('#!/usr/bin/python\n#coding:utf-8\n def ISA():\n print("慎言善思,学以致用")\nif __name__ == "__main__":\n ISA()',msg['FromUserName']) itchat.send_msg('欢迎关注我们的公众号,\n“创软俱乐部ISA”',msg['FromUserName']) itchat.send_image('QR_ISA.jpg', msg['FromUserName'])#发送公众号的二维码  else:      itchat.send_msg(u'%s' % tuling(msg['Text']), msg['FromUserName'])
 

最后,这个主要是用来在学校社团招新的时候应用的

猜你喜欢

转载自www.cnblogs.com/s42-/p/9613984.html