M
M
mrgyf12016-10-21 14:55:16
PHP
mrgyf1, 2016-10-21 14:55:16

How to accept user response in Telegram Bot?

Good day, colleagues! I just started learning the PHP language and set myself the difficult task of writing a Telegram Bot for corporate needs. On a free hosting, I raised WebHooks and it even works and accepts requests.
Now the bottom line: could any of you tell me how to take a response from the user and put it in a variable? That is,
B- bot, P-user
B - Good afternoon, for registration, specify GENDER (M, F)
P - M
B- Introduce yourself (full name)
P - Ivanov Ivan Ivanovich
And at the output have $gender = male, $ in the script name = 'Ivanov Ivan Ivanovich', well, then put the data into the database.
Every time I encounter the problem that my bot does not wait for a response from the user, but simply pours messages, of course, without writing down the answers anywhere.
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nllm, 2016-10-21
@mrgyf1

Show the source of the bot.
You need to understand that there are 2 components to a webhook. One is to receive messages from the user (to the webhook itself), the second is to send messages to the user via the bot api.
You need to capture the states of the bot for each user (to process and save your survey answers)
Here is an example of a bot that I made to demonstrate at the webinar. The bot is simple but functional. See as an example.
https://www.dropbox.com/s/wjsu99zx3pvpee3/demobot....

L
Lulzsec, 2016-10-21
@Lulzsec

There is a good library here: https://github.com/irazasyed/telegram-bot-sdk
There are examples in the documentation. This is for you:

include('db.php'); //путь до конфигурации БД
include('vendor/autoload.php'); //Путь до файла-автозагрузчика библиотеки
use Telegram\Bot\Api;
$telegram = new Api(''); //токен в одинарных кавычках
$result = $telegram -> getWebhookUpdates();
$text = $result["message"]["text"];
$chat_id = $result["message"]["chat"]["id"];
$name = $result["message"]["from"]["username"];

if ($text == "/start") {
$reply = "Спасибо за подписку!";
$response = $telegram->sendMessage([ 'chat_id' => $chat_id, 'text' => $reply ]);
mysqli_query($db, "INSERT INTO users (id, username, chatid) VALUES (NULL, '$name', $chat_id)"); //добавление в базу
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question