Transformer课程 业务对话机器人Rasa 3.x 微信+Rasa机器人实战案例

Transformer课程 业务对话机器人Rasa 3.x 微信+Rasa机器人实战案例

Transformer课程 业务对话机器人Rasa 3.x 微信+Rasa机器人实战案例

Rasa官网


www.zeeklog.com  - Transformer课程 业务对话机器人Rasa 3.x 微信+Rasa机器人实战案例

Rasa 服务器启动

(installingrasa) E:\starspace\my_rasa>rasa run --enable-api

(installingrasa) E:\starspace\my_rasa>
(installingrasa) E:\starspace\my_rasa>rasa run  --enable-api
2022-01-15 11:26:47 INFO     root  - Starting Rasa server on http://0.0.0.0:5005
2022-01-15 11:26:50 INFO     rasa.core.processor  - Loading model models\20220103-163343-calm-developer.tar.gz...
2022-01-15 11:27:25 WARNING  rasa.shared.utils.common  - The UnexpecTED Intent Policy is currently experimental and might change or be removed in the future 🔬 Please share your feedback on it in the forum (https://forum.rasa.com) to help us make this feature ready for production.
2022-01-15 11:27:44 INFO     root  - Rasa server is up and running.

运行结果

www.zeeklog.com  - Transformer课程 业务对话机器人Rasa 3.x 微信+Rasa机器人实战案例

使用postman客户端测试 localhost:5005/model/parse/

www.zeeklog.com  - Transformer课程 业务对话机器人Rasa 3.x 微信+Rasa机器人实战案例

客户端发送请求给Rasa

import requests
import json

url = "localhost:5005/model/parse/"

payload = json.dumps({
  "text": "hello is anybody there"
})
headers = {
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

运行结果

www.zeeklog.com  - Transformer课程 业务对话机器人Rasa 3.x 微信+Rasa机器人实战案例

微信客户端接入Rasa

采用互联网大佬wxpy库实现的微信机器人
[wxpy官方文档]
有趣的事,Rasa从来不会缺席!

from wxpy import *

import wx_reply
import wx_command
import load

 
bot = Bot(cache_path=True)
# 加载配置信息到机器人
load.load_config_to_bot(bot)


"""好友功能"""
@bot.register(msg_types=FRIENDS)
def auto_accept_friends(msg):
    """自动接受好友请求"""
    wx_reply.auto_accept_friends(msg)


@bot.register(chats=Friend)
def friend_msg(msg):
    """接收好友消息"""
    if not msg.bot.is_friend_auto_reply:
        return None
    if msg.type == TEXT:
        wx_reply.auto_reply(msg)
        return None
    elif msg.type == RECORDING:
        return '不停不停,王八念经'
    else:
        pass


"""群功能"""
@bot.register(chats=Group)
def group_msg(msg):
    """接收群消息"""
    # 群@转发功能
    if msg.is_at and msg.bot.is_forward_group_at_msg:
        msg.forward(msg.bot.master, prefix='「{0}」在群「{1}」中艾特了你:'.format(msg.member.name, msg.chat.name))

    if msg.type == TEXT:
        # 群回复
        if msg.bot.is_group_reply:
            if msg.bot.is_group_at_reply:
                # @机器人才回复
                if msg.is_at:
                    wx_reply.auto_reply(msg)
            else:
                # 不用@直接回复
                wx_reply.auto_reply(msg)
    elif msg.type == SHARING and msg.bot.is_listen_sharing and msg.chat in msg.bot.listen_sharing_groups:
        # 群分享转发监控,防止分享广告
        msg.forward(msg.bot.master, prefix='分享监控:「{0}」在「{1}」分享了:'.format(msg.member.name, msg.chat.name))
    else:
        pass
    # 监听好友群聊,如老板讲话
    if msg.bot.is_listen_friend and msg.chat in msg.bot.listen_friend_groups and msg.member.is_friend in msg.bot.listen_friends:
        msg.forward(msg.bot.master, prefix='监听指定好友群消息:「{0}」在「{1}」发了消息:'.format(msg.member.is_friend.remark_name or msg.member.nick_name, msg.chat.name))
    return None


@bot.register(msg_types=NOTE)
def system_msg(msg):
    """接收系统消息"""
    wx_reply.handle_system_msg(msg)


"""管理员功能"""
@bot.register(chats=bot.master)
def do_command(msg):
    """执行管理员命令"""
    wx_command.do_command(msg)


# 堵塞进程,直到结束消息监听 (例如,机器人被登出时)
# embed() 互交模式阻塞,电脑休眠或关闭互交窗口则退出程序
bot.join()
www.zeeklog.com  - Transformer课程 业务对话机器人Rasa 3.x 微信+Rasa机器人实战案例

运行结果:

www.zeeklog.com  - Transformer课程 业务对话机器人Rasa 3.x 微信+Rasa机器人实战案例

Rasa系列博客:

Could not load content