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/smooch/smooch-bot-example
Say I modify the script https://github.com/smooch/smooch-bot-example/blob/master/script.js as follows
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?
My approach was add
stateMachine.setState(postback.action.payload)
to the handlePostback method of github.com/smooch/smooch-bot-example/blob/master/heroku/index.js
However, that threw an error code H12. I also experimented with
stateMachine.transition(postback.action,postback.action.payload)
to no avail.
If you want to advance the conversation based on a postback you'll have to first output the buttons from the bot's prompt (so you can handle the button click in the receive), modify the
handlePostback
function inindex.js
, then handle the user's "reply" in your receive method - try this - modifyscript.js
like so:Then modify the
handlePostback
function inindex.js
so that it treats a postback like a regular message:Now when a user clicks your button it will be pushed to the stateMachine and handled like a reply.
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 insideindex.js
, replacing the existinghandlePostback
function in the smooch-bot-example GitHub repo: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 thecase
list like so :Note that you should not write
break;
at the end of eachcase
if the outcome you want is the same (here : setting the state and prompting the corresponding message).If you want to handle other postbacks differently, you can add cases after the
break;
statement and do other stuff instead.Hope this helps!
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:Alternatively, you could have your
handlePostback
implementation callstateMachine.setState(state)
andstateMachine.prompt(state)
independently, if you wanted to have your postbacks behave differently from regular text responses.