如何在Rivescript-Python中预处理特殊触发器?

时间:2017-12-28 12:22:01

标签: rivescript

我的机器人将用户的名称存储在数据库中,并且数据库有时会向会话发送消息“setname username”。样本rivescript:

+ setname *
- <set name=<formal>>

+ (what is my name|who am i)
- You're <get name>, right?

+ didntlike
- {topic=nlike}Why?

> topic nlike

+ *
- {topic=random}Thanks for charing.

< topic

+ *
- I don't have a reply for that.
- Try asking that a different way.

问题是,当用户处于* nlike'之类的主题时,我会发送消息来设置名称,然后会话退出主题。

期待的对话:

Me: hello
Bot: I don't have a reply for that.
Me: didntlike
Bot: Why?
Me: setname John
Bot:
Me: I didn't like because you are ugly.
Bot: Thanks for charing.
Me: Who am I?
Bot: You're John, right?

有没有办法在开始区块中对待它?我尝试了不同的语法,但没有积极的结果。我想到了类似的东西:

> begin

+ setname *
- <set name=<formal>>

+ request
- {ok}

< begin

一种解决方法是在所有主题中添加相同的触发器,但我需要一个更好的解决方案,因为当我的铆接文件变大时,这种方法容易出错。

====根据尼尔森的回答尝试了代码=====

> begin
    + request
    - {ok}{topic=specialtriggers}
< begin

> topic specialtriggers

+ setname *
- <set name=<formal>>

< topic

+ (what is my name|who am i)
- You're <get name>, right?

+ didntlike
- {topic=nlike}Why?

> topic nlike

+ *
- {topic=random}Thanks for charing.

< topic

+ *
- I don't have a reply for that.
- Try asking that a different way.

我认为将主题添加到{ok} {topic = specialtriggers}使得此主题之外的所有触发器都失败。在预处理之后,Rivescript应该回答是否是特殊触发器,或者搜索正常触发器。

2 个答案:

答案 0 :(得分:0)

如果您的问题仅是关于如何使用begin块始终预处理用户消息,答案是:

  • 不需要在开始块中输入触发器。 + request是任何用户消息的通用表示。
  • 如果您希望限制触发器在当前主题的开始块 中匹配,则{ok}标记就足够了。

修改您的代码,如下所示

+ didntlike
- {topic=nlike}Why?

> topic nlike
    + *
    - Thanks for charing.
< topic

> begin
    + request
    - {ok} (Preprocessed in begin)
< begin

尝试以下消息序列didntlikehello,您会看到回复附加了(Preprocessed in begin)

  • 否则,在开始块中的回复需要添加一个主题,其中(哪个主题)来获取触发器并从中回复。如果您想在setname主题中获取random的触发器,则可以将begin块添加到第一个代码段的末尾。

RiveScript代码

> begin
    + request
    - {ok}{topic=random}
< begin

在这种情况下,向setname提交请求后的主题是主题random

答案 1 :(得分:0)

根据Noah Petherbridge's response上的chatbots.org回答,一个可行的解决方案是使用主题继承:

> topic specialtriggers
    + setname *{weight=9991234}
    - <set name=<star1>>
< topic

> topic random includes specialtriggers
    // you don't actually need to put anything inside here, since triggers
    // without a topic are in the "random" topic automatically, but this
    // topic declaration line will make "random" include "important"
< topic

// but for your other topics, include the specialtriggers one
> topic nlike includes specialtriggers
    + *
    - {topic=random}Thanks for caring.
< topic