L
L
lokidex2019-01-28 21:39:00
PHP
lokidex, 2019-01-28 21:39:00

How to handle viber bot responses?

The question is next. I can’t understand, it’s more accurate to organize the logic of the chat bot in viber (I don’t think it’s worth linking specifically to viber in this matter, but since I’m doing it, I wrote about it).
Processing for a specific message from the user is not a problem by catching $viber->message->text.
Having made a check for compliance or occurrence.
For example:
if ($viber->message->text=='hello') { send response .... hello user, how are u? .... } or via strpos
But, the question is the following. For example, you need to edit user data in the database.
1. Display a list of buttons, each corresponding to a user.
2. Click on the button, the message went into processing, for example "edituser12" (where 12 is for example the user id)
3. We caught the message, found the occurrence of "edituser", determined the id, we actually show the message, for example, "Enter a new user's full name."
Now here's the question. Some new full name will be entered, and sent for processing by message. How to determine which user should be edited?
I did it through cookies, that is, at the 3rd step I write in cookies that the process of editing the user, user id is now in progress, and accordingly the next message will be the user's full name. But, it seems to me that doing this through cookies is not correct, and there is a more correct option, since this one is not stable. But I still can’t understand how, although it seems to me that the answer is on the surface and very simple.
I hope someone understands my question and will help a competent answer :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AleksMo, 2019-04-05
@AleksMo

When a user writes to a public (chat), his id is determined (yI8UmH + jb9ZAzyYtU / mYwg ==),
the ID can be written to a table.
If he sent a message - change the name (example: EditName), then we look for the corresponding ID in the table and write down the new full name (request - update), but this is only for your database.
In the viber, the name will only be the one indicated by the user.
<?php
# array with user data ...
$json = file_get_contents('php://input');
$viber = json_decode($json);
//$sender=$viber->sender->id;//ID To send a message
$sender = $viber->{'sender'}; // sender is an array with user data
$name_user=$sender->name;//Name
$avatar = $sender->avatar; //Picture
$receiverID = $sender->id; // ID
# Message from user
$message=$viber->message->text;
if ($receiverID!= "") //If the ID is not empty, then we save the user to the database
{
# Write to the database the data about the user who subscribed ....
# Connect to the database
include 'config.php';
$db = mysql_connect($servername, $username, $password);
if (!$db) {die('Connection error: ' . mysql_error());}
mysql_select_db($dbname, $db);
/*Making a database query*/
$result = mysql_query ("INSERT IGNORE INTO ViberTab (receiver,name, avatar) VALUES ('$receiverID','$name_user', '$avatar')");
mysql_close($db);
if ($message == ' EditName ') //If message is equal to change name
{
include 'config.php';
$db = mysql_connect($servername, $username, $password);
if (!$db) {die('Connection error: ' . mysql_error());}
mysql_select_db($dbname, $db);
/*Make a query to the database*/
$result = mysql_query ("UPDATE ViberTab SET name =$viber->message->text WHERE id=$receiverID ");
mysql_close($db);
}
?>
This is an example to think about... maybe there are more options....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question