forked from Rowl1ng/rasa_wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat.py
48 lines (40 loc) · 1.5 KB
/
wechat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from wxpy import *
from rasa_core.agent import Agent
from rasa_core.interpreter import RasaNLUInterpreter
# from wxpy import get_wechat_logger
import sys
# parent = os.path.dirname(os.path.realpath(__file__))
sys.path.append('/root/MITIE/mitielib')
# 之前训练好的NLU模型
nlu_model_path = '../models/nlu/model_20171109-164837'
agent = Agent.load("../models/policy/mom", interpreter=RasaNLUInterpreter(nlu_model_path))
# 初始化机器人,扫码登陆
bot = Bot(console_qr=True, cache_path=True)
# bot = Bot(console_qr=False, cache_path=True)
# bot.self.add()
# bot.self.accept()
# bot.self.send('哈咯~')
# bot.file_helper.send('哈咯~')
# logger = get_wechat_logger()
# 自动接受新的好友请求
@bot.register(msg_types=FRIENDS)
def auto_accept_friends(msg):
# 接受好友请求
new_friend = msg.card.accept()
# 向新的好友发送消息
new_friend.send('哈哈,我自动接受了你的好友请求')
# 回复 my_friend 的消息 (优先匹配后注册的函数!)
# @bot.register(my_friend)
# def reply_my_friend(msg):
# return 'received: {} ({})'.format(msg.text, msg.type)
@bot.register(bot.self, except_self=False)
def reply_self(msg):
# agent = Agent.load("models/policy/current",
# interpreter=RasaNLUInterpreter(nlu_model_path))
ans = agent.handle_message(msg.text)
print(ans)
# msg.reply(ans)
return " ".join(ans)
# return 'received: {} ({})'.format(msg.text, msg.type)
# 进入 Python 命令行、让程序保持运行
embed()