wxpy——聊天機器人的實現


一、安裝wxpy包

通過cmd中輸入   pip install -U wxpy -i "https://pypi.doubanio.com/simple/"   進行安裝

二、在python中登錄微信web版

1 from wxpy import *
2 #初始化執行登陸操作,需要手機掃描登陸   bot = Bot()
3 #自動保存登陸信息,不再需要掃描二維碼
4 bot = Bot(cache_path=True)

三、部分簡單功能

#給文件助手發送消息
bot.file_helper.send('hi! 助手')   

#指定聊天對象.處需要使用備注名!(盡量唯一)
my_friend = bot.friends().search('xxxx')[0]
my_friend.send('你干啥呢')

#自動回復
my_friend = bot.friends().search('xxxx')[0]
@bot.register(my_friend)
def reply(msg):
    return'目前較忙,已收到您的消息,空閑時將回復' 

 四、智能機器人回復

需要在http://www.tuling123.com上注冊賬號,通過圖靈機器人完成初級的智能回復,比如詢問天氣等等

from wxpy import *
import requests
import json

bot = Bot(cache_path=True)

def auto_replay(text):
    url = "http://www.tuling123.com/openapi/api"
    api_key = "7*****************b"
    payload = {'key':api_key,
               'info':text,
               'userid':'rebot'}
    r = requests.post(url, data=json.dumps(payload))
    result = json.loads(r.content)
    return result['text']
#注意!這是所有人都回復
@bot.register()
def print_message(msg):
    #print(msg.text)
    return auto_replay(msg.text)

五、指定群聊/人自動回復

#指定群聊
group = bot.groups().search('群聊名')[0]
#指定好友
friend = bot.friends().search('好友名')[0]
#按需求修改參數
@bot.register(group,msg_types=TEXT)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM