Smooch:如何做回发依赖状态转换? [英] Smooch: How to do postback dependent state transition?

查看:116
本文介绍了Smooch:如何做回发依赖状态转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to transition the script from one state to another based on Smooch postback payloads; but getting error code 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'
},

意图是机器人的状态会根据回发有效负载。

The intention is that the's bot's state would transition depending on the postback payload.

问题是,如何实现这一目标?

Question is, how do I make that happen?

我的方法是添加

stateMachine.setState(postback.action.payload)

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

to the handlePostback method of github.com/smooch/smooch-bot-example/blob/master/heroku/index.js

但是,它抛出了错误代码H12。我还尝试了

However, that threw an error code H12. I also experimented with

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

无济于事。

推荐答案

我在[object Object]而不是字符串中遇到了同样的问题。这是因为你使用函数获取或设置的状态包含在一个对象中,而不是一个字符串......我用中的代码修复了它index.js ,替换 handlePostback 函数example / blob / master / heroku / index.js #L109rel =nofollow> smooch-bot-example GitHub repo:

I got the same issue with the [object Object] instead of a string. This is because the state you get or set with a function is contained in an object, not a string... I fixed it with this code inside index.js, replacing the existing handlePostback function in the smooch-bot-example GitHub repo:

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 列表中,如下所示:

Then inside script.js all you need to do is define states corresponding to the exact postback payloads. If you have multiple postbacks that should take the user to other states, just add them to the case list like so :

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;

请注意,您不应该写 break; 在每个案例的末尾,如果你想要的结果是相同的(这里:设置状态并提示相应的消息)。

Note that you should not write break; at the end of each case if the outcome you want is the same (here : setting the state and prompting the corresponding message).

如果你想以不同的方式处理其他回发,你可以在 break; 语句之后添加案例,而不是做其他事情。

If you want to handle other postbacks differently, you can add cases after the break; statement and do other stuff instead.

希望这会有所帮助!

这篇关于Smooch:如何做回发依赖状态转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆