V
V
Vitaly2020-07-08 13:18:42
Node.js
Vitaly, 2020-07-08 13:18:42

Why does ConversationStartedOnFinishCallback not work correctly in Viber bot?

All the best! While writing a small bot for Viber, I encountered a problem when calling the onFinish callback in the onConversationStarted method

https://github.com/Viber/viber-bot-node#Conversati...

In principle, the code from the example works

bot.onConversationStarted((userProfile, isSubscribed, context, onFinish) =>
  onFinish(new TextMessage(`Hi, ${userProfile.name}! Nice to meet you.`)));


but if before onFinish you try to make some asynchronous request to the database or somewhere else, you get problems

bot.onConversationStarted((userProfile, isSubscribed, context, onFinish) =>

  doSomeRequest(context)
    .then(responseData =>{
      onFinish(new TextMessage(`Hi! here is data from responseData ${responseData}`))
    })

);


In this case, after the first launch of the bot (not the server) , onFinish does not work (the bot does not send messages), but if you then try to contact the bot, onFinish already works (the bot sent messages).

Further it is even more interesting, I observe something like a cache.

When onFinish still worked, the bot sent messages, it turns out that the bot sent messages with old data, that is, the data of the previous message.

That is, if in onConversationStarted in the current session/request to the bot , say, 2 was passed to the context parameter , then this value was passed to the doSomeRequest request . and in the response responseDatareceived something like
responseData2 , then if before that there was a request to the bot, but with context equal to 1 , then despite the fact that in this request we work with input data, the values ​​of context , which are now equal to 2 , then onFinish and, respectively , messages in the bot, I get with the result 1 .

Please let me know if anyone else has experienced something similar?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy, 2020-09-13
@Scorpiored88

Yes, I figured it out, you need to call sendMessage instead of onFinish, and everything works fine

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question