After having an overwhelming response to our earlier post on building Viber Chat bot, we are here again with another Viber chat bot article. In that post, we provided you with set of code for receiving message from users and deliver normal text messages. Rather than just replying text, we can also send complex messages. So we came up with this article to fulfill this gap. This time, we will be sending structured message to the user including keyboard menu, pictures and links as well.
We will develop a Viber chat bot similar to that of chat bot of LetzCricket.
Finding Chat bot
First of all, we are going to find how the chat bot of LetzCricket looks like. For this, we need to search for a public account named LetzCricket on Viber.
You will get the screen similar the following screenshot if you enter the public chat and subscribe to the public account.
Now click on the message icon in top right side of the screen to start 1-on-1 chat. Then message “Hi” to start the conversation. As a result, you will see the following message with keyboard menu.
Now that we have a preview of what we are going to make, lets start coding. Here’s the PHP code:
Creating Keyboard menu
We sent normal text in the previous article. In this article, we are sending keyboard menu along with message. Keyboards can be attached to any message type and be sent and displayed together. To attach a keyboard to a message simply add the keyboard’s parameters to the message JSON. Please refer to previous article to see keyboard’s parameter.
The keyboards are fully customizable. We can create and design according to our requirement. The client will always display the last keyboard that was sent to it.
We have created a function getMainMenu() to create keyboard menu. It will be called at the same place where we had prepared content to deliver to the user.
The getMainMenu() function that delivers the main menu to the user would look like this:
This will show the keyboard menu with custom design.
Now if you tap the News option from the keyboard menu, a message “LCNews” is sent to the webhook. We can easily detect if a user has tapped the News option and fetch the news to send it to the user.
The function to send news list to the user is as follows:
This will display list of news back to the user like:
Similarly you can send list of articles, gallery or interviews in the format you like.
Hope you enjoy this complete tutorial. Please do write to us if you face any problem. We might as well continue with the other features if you want. Don’t forget to provide feedback on what you would like us to deliver to you in the continuity of this tutorial.