Smooch: How to do postback dependent state transition?

后端 未结 3 982
挽巷
挽巷 2021-01-20 19:42

I am trying to transition the script from one state to another based on Smooch postback payloads; but getting error code H12.

Consider the example https://github.com

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-20 19:49

    Postbacks won't automatically transition your conversation from one state to the next, you have to write that logic yourself. Luckily the smooch-bot-example you're using already has a postback handler defined here:

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

    So whatever transition logic you want should go in there. You can do this by creating a stateMachine and calling receiveMessage() on it the same way handleMessages() already works. For example:

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

    Alternatively, you could have your handlePostback implementation call stateMachine.setState(state) and stateMachine.prompt(state) independently, if you wanted to have your postbacks behave differently from regular text responses.

提交回复
热议问题