python教你如何把自己的微信变成机器人
如何把自己的微信变成机器人先看效果图,我把搜的微信小号变成了机器人,然后用微信大号和它进行对话,对话截图如下:后台打印的数据如下:
如何把自己的微信变成机器人
先看效果图,我把搜的微信小号变成了机器人,然后用微信大号和它进行对话,对话截图如下:
后台打印的数据如下:
实验准备:
1.wxpy库,安装只需一部:sudo apt-get install wxpy
2.注册欧拉蜜人工智能开放平台,这是地址,并创建一个应用。
完整代码:
# -*- coding: utf-8 -*-
# @Time : 2019/8/30 上午9:26
# @Author : lixing
# @Email : jingyoushui@163.com
# @File : wechat2.py
# @Software: PyCharm
"#codeing=utf-8"
import json
import requests
from wxpy import *
from NluAPISample import NluAPISample
import argparse
import re
def olami(text):
url = "https://cn.olami.ai/cloudservice/api"
appKey = "你的appKey"
appSecret = "你的App Secret"
nluApi = NluAPISample()
nluApi.setLocalization(url)
nluApi.setAuthorization(appKey, appSecret)
result = json.loads(nluApi.getRecognitionResult(nluApi.API_NAME_NLI, text))
# print(type(result))
print("\nResult:\n\n",result)
content = ""
t = result['data']['nli'][0]
type = result['data']['nli'][0]['type']
if(type=="joke"):
content = t['data_obj'][0]['content']
if(type=="selection"):
item = []
for i in t['data_obj']:
item.append("名称:"+i['hotel_name'])
item.append("地址:" + i['hotel_address'])
item.append("链接:" + i['description_url'])
content = "\n".join(item)
if(type=="openweb"):
content = t['data_obj'][0]['url']
if(type == "baike"):
item = []
content = t['data_obj'][0]['description']
item_id = t['data_obj'][0]['field_name']
item_value = t['data_obj'][0]['field_value']
for i in item_id:
item.append(i +":"+item_value[item_id.index(i)])
items = "\n".join(item)
content = content+("\n"+items)
return str(result['data']['nli'][0]['desc_obj']['result'])+("\n"+content)
bot = Bot(cache_path=True,console_qr=True) # 登录缓存
found = bot.friends().search('某某1')
obj1 = found[0]
print(type(obj1))
found2 = bot.friends().search('某某2')
obj2 = found2[0]
print(obj2)
found3 = bot.groups().search('群1')
print(found3)
obj3 = found3[0]
user = [obj1,obj2,obj3]
print(user)
@bot.register(user)
def message(msg):
print('msg:' + str(msg))
if(msg.text):
ret = olami(msg.text)
res1 = re.sub("欧拉蜜","XXX",ret)
res2 = re.sub("7岁", "xx岁", res1)
return res2
else:
ret = "我现在还只能支持文字呦"
return ret
# embed()
if __name__ == '__main__':
# 堵塞线程
bot.join()
代码详解:appKey appSecret
换成olami平台上自己创建的应用的,如图所示:
olami接口返回的json格式数据如下:
{
"nli": [
{
"desc_obj": {
"result": "好的,那你听好喽。",
"name": "",
"type": "joke",
"status": 0
},
"data_obj": [
{
"content": "某日,生物老师问:“没有尾巴的是什么熊?” 某生说:“无尾熊。” 老师问:“没有脖子的是什么熊?” 某生说:“无脖熊。” 老师再问:“没鸡鸡的是什么熊?” 某生答:“无鸟熊。” 老师:“错!” 某生再答:“嗯~~~无鸡熊。” 老师:“错!唉……是母熊嘛!……现在的小孩……!!” "
}
],
"type": "joke"
}
]
}
其中type不同,json的Key也不同,根据以上json字段,取出content的代码如下:
t = result['data']['nli'][0]
type = result['data']['nli'][0]['type']
if(type=="joke"):
content = t['data_obj'][0]['content']
依次type还有selection,openweb,baike等类型的
if(type=="selection"):
item = []
for i in t['data_obj']:
item.append("名称:"+i['hotel_name'])
item.append("地址:" + i['hotel_address'])
item.append("链接:" + i['description_url'])
content = "\n".join(item)
if(type=="openweb"):
content = t['data_obj'][0]['url']
if(type == "baike"):
item = []
content = t['data_obj'][0]['description']
item_id = t['data_obj'][0]['field_name']
item_value = t['data_obj'][0]['field_value']
for i in item_id:
item.append(i +":"+item_value[item_id.index(i)])
items = "\n".join(item)
content = content+("\n"+items)
@bot.register(user)
用来注册监听的用户,可以是好友,也可以是群,如果是单个用户或群:
user = bot.friends().search('某某1')
@bot.register(user)
def message(msg):
如果是监听多个用户和群,需要放在一个列表里,但是user本身就是一个列表,user类型是<class 'wxpy.api.chats.chats.Chats'>
,user的值是[<Friend: 某某1>]
需要将它第一个元素取出来,然后再一起放在一个列表里。像这样[<Friend: 苹果粥>, <Friend: 静幽水>, <Group: 都是我呀>]
。
found = bot.friends().search('苹果粥')
obj1 = found[0]
found2 = bot.friends().search('静幽水')
obj2 = found2[0]
found3 = bot.groups().search('都是我呀')
obj3 = found3[0]
user = [obj1,obj2,obj3]
print(user)
@bot.register(user)
def message(msg):
当微信接收到文本信息的时候,会调用olami的api,然后返回微信就会自动回复。当然可以把欧拉蜜
换成自己的名字。
@bot.register(user)
def message(msg):
print('msg:' + str(msg))
if(msg.text):
ret = olami(msg.text)
res1 = re.sub("欧拉蜜","XXX",ret)
res2 = re.sub("7岁", "xx岁", res1)
return res2
else:
ret = "我现在还只能支持文字呦"
return ret

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)