Sending structured message and keyboard menu in Viber chat bot


In this post, 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:

$request = file_get_contents("php://input");
$input = json_decode($request, true);
if($input['event'] == 'webhook') {
  echo json_encode($webhook_response);
else if($input['event'] == "subscribed") {
  // when a user subscribes to the public account
else if($input['event'] == "conversation_started"){
  // when a conversation is started
elseif($input['event'] == "message") {
  /* when a user message is received */
  $type = $input['message']['type']; //type of message received (text/picture)
  $text = $input['message']['text']; //actual message the user has sent
  $sender_id = $input['sender']['id']; //unique viber id of user who sent the message
  $sender_name = $input['sender']['name']; //name of the user who sent the message
  // here goes the data to send message back to the user
if($text == 'LCNews'){
  //if user presses News option
  $data = getNewsList($sender_id);
  //if user presses Articles option
  $data = getArticleList($sender_id);
elseif($text==' LCInterviews'){
  //if user presses Interview option
  $data = getInterviewList($sender_id);
elseif($text==' LCGallery'){
  //if user presses Gallery option
  $data = getGalleryList($sender_id);
elseif($text==' LCPoll'){
  //if user presses Poll option
  $data = getPollList($sender_id);
elseif($text==' LCPOTM'){
  //if user presses Player of the month option
  $data = getPOTM($sender_id);
elseif($text==' LCQuote'){
  //if user presses Quote option
  $data = getQuote($sender_id);
  $data = getMainMenu($sender_id);
  //here goes the curl to send data to user
  $ch = curl_init(" ");
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  $result = curl_exec($ch);

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.

ALSO READ  Generate User Avatar with Name using PHP

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:

function getMainMenu($user_id){
  $data['text']="Please select one of the options below:";
  $potm['Text']='PLAYER OF THE MONTH';
  $quote['Text']='QUOTE OF THE DAY';
  $website['Text']='VISITE OUR WEBSITE';

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:

function getNewsList($user_id){
    //fetch news array from wherever you like from your database or by calling any api
    // your news list will be something like this:
    $newsArray = array(
          'title'=>'The Title',
    $data['text']="Here are some news from LetzCricket:";

This will display list of news back to the user like:

ALSO READ  Generate QR Code using PHP Library

Similarly you can send list of articles, gallery or interviews in the format you like.

You can refer the following video for complete guidance :


Comments are closed.