Smooch:如何进行依赖于回发的状态转换

Smooch: How to do postback dependent state transition?

本文关键字:状态 转换 依赖于 何进行 Smooch      更新时间:2023-09-26

我正在尝试基于Smooch回发有效负载将脚本从一种状态转换到另一种状态;但是得到错误代码H12。

考虑一下这个例子https://github.com/smooch/smooch-bot-example

假设我修改了脚本https://github.com/smooch/smooch-bot-example/blob/master/script.js如下

start: {
    receive: (bot) => {
        return bot.say('Hi! I''m Smooch Bot! Continue? %[Yes](postback:askName) %[No](postback:bye) );
    }
},
bye: {
    prompt: (bot) => bot.say('Pleasure meeting you'),
    receive: () => 'processing'
},

其意图是,机器人的状态将根据回发负载进行转换。

问题是,我该如何做到这一点?

我的方法是添加

stateMachine.setState(postback.action.payload)

到github.com/smooch/smooch-bot-example/blob/master/heroku/index.js 的handlePostback方法

然而,这引发了一个错误代码H12。我还试验了

stateMachine.transition(postback.action,postback.action.payload)

但无济于事。

我在使用[object object]而不是字符串时遇到了同样的问题。这是因为用函数获取或设置的state包含在对象中,而不是字符串中。。。我在index.js中用这段代码修复了它,替换了smoch bot示例GitHub repo:中现有的handlePostback函数

function handlePostback(req, res) {
const stateMachine = new StateMachine({
    script,
    bot: createBot(req.body.appUser)
});
const postback = req.body.postbacks[0];
if (!postback || !postback.action) {
    res.end();
};
const smoochPayload = postback.action.payload;
// Change conversation state according to postback clicked
switch (smoochPayload) {
    case "POSTBACK-PAYLOAD":
        Promise.all([
            stateMachine.bot.releaseLock(),
            stateMachine.setState(smoochPayload), // set new state
            stateMachine.prompt(smoochPayload) // call state prompt() if any
        ]);
        res.end();
    break;
    default:
        stateMachine.bot.say("POSTBACK ISN'T RECOGNIZED") // for testing purposes
            .then(() => res.end());
};
}

然后在script.js中,您所需要做的就是定义与确切的回发有效负载相对应的状态。如果有多个回发应该将用户带到其他状态,只需将它们添加到case列表中,如下所示:

case "POSTBACK-PAYLOAD-1":
case "POSTBACK-PAYLOAD-2":
case "POSTBACK-PAYLOAD-3":
case "POSTBACK-PAYLOAD-4":
Promise.all([
        stateMachine.bot.releaseLock(),
        stateMachine.setState(smoochPayload), // set new state
        stateMachine.prompt(smoochPayload) // call state prompt() if any
    ]);
    res.end();
break;

请注意,如果您想要的结果相同(此处:设置状态并提示相应的消息),则不应在每个case的末尾写入break;

如果您想以不同的方式处理其他回发,可以在break;语句之后添加case,然后执行其他操作。

希望这能有所帮助!

Postbacks不会自动将您的对话从一种状态转换到下一种状态,您必须自己编写该逻辑。幸运的是,您使用的接吻机器人示例已经在这里定义了一个回发处理程序:

https://github.com/smooch/smooch-bot-example/blob/30d2fc6/heroku/index.js#L115

所以,无论你想要什么样的转换逻辑,都应该在那里。您可以通过创建一个stateMachine并在其上调用receiveMessage()来实现这一点,方法与handleMessages()的工作方式相同。例如:

const stateMachine = new StateMachine({
    script,
    bot: createBot(req.body.appUser)
});
stateMachine.receiveMessage({
    text: 'whatever your script expects'
})

或者,如果您希望回发的行为与常规文本响应不同,您可以让handlePostback实现独立地调用stateMachine.setState(state)stateMachine.prompt(state)

如果你想在回发的基础上推进对话,你必须首先从机器人的提示中输出按钮(这样你就可以处理接收中的按钮点击),修改index.js中的handlePostback功能,然后在你的接收方法中处理用户的"回复"-试试这个-这样修改script.js

start: {
    prompt: (bot) => bot.say(`Hi! I'm Smooch Bot! Continue? %[Yes](postback:askName) %[No](postback:bye)`),
    receive: (bot, message) => {
      switch(message.text) {
        case 'Yes':
          return bot.say(`Ok, great!`)
            .then(() => 'hi')
          break;
        case 'No':
          return bot.say(`Ok, no prob!`)
            .then(() => 'bye')
          break;
        default:
          return bot.say(`hmm...`)
            .then(() => 'processing')
          break;          
      }
    }
},
hi: {
    prompt: (bot) => bot.say('Pleasure meeting you'),
    receive: () => 'processing'
},
bye: {
    prompt: (bot) => bot.say('Pleasure meeting you'),
    receive: () => 'processing'
},

然后修改index.js中的handlePostback函数,使其将回发处理为常规消息:

function handlePostback(req, res) {
    const postback = req.body.postbacks[0];
    if (!postback || !postback.action)
        res.end();
    const stateMachine = new StateMachine({
        script,
        bot: createBot(req.body.appUser)
    });
    const msg = postback;
    // if you want the payload instead just do msg.action.paylod
    msg.text = msg.action.text;
    stateMachine.receiveMessage(msg)
      .then(() => res.end())
      .catch((err) => {
        console.error('SmoochBot error:', err);
        res.end();
      });
}

现在,当用户单击您的按钮时,它将被推到stateMachine,并像回复一样进行处理。