E
E
EVG822020-10-21 13:36:09
PHP
EVG82, 2020-10-21 13:36:09

How to insert the value of the first_name variable into the text in a telegram bot?

Here is part of the code

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

header('Content-Type: text/html; charset=utf-8');
// подрубаем API
require_once("vendor/autoload.php");
// создаем переменную бота
$token = "токен";
$bot = new \TelegramBot\Api\Client($token);

// если бот еще не зарегистрирован - регистрируем
if(!file_exists("registered.trigger")){ 
    /**
     * файл registered.trigger будет создаваться после регистрации бота. 
     * если этого файла нет значит бот не зарегистрирован 
     */
     
    // URl текущей страницы
    $page_url = "https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    $result = $bot->setWebhook($page_url);
    if($result){
        file_put_contents("registered.trigger",time()); // создаем файл дабы прекратить повторные регистрации
    }
}

// Кнопки у сообщений
//================ вызывается команда бота появляется сообщение и под ним кнопка "смотреть видео"============
$bot->command('start', function ($message) use ($bot) {
    $keyboard = new \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup(
        [
            [
                ['callback_data' => 'data_test', 'text' => 'Смотреть видео '],
                

            ]
        ]
//  'one_time_keyboard' => false, 
      //  'resize_keyboard' => true
    );
   $first_name = " типа имя";
   $bot->sendMessage($message->getChat()->getId(), "Здравствуйте, ".$first_name."!


As I understand from what I found on the Internet, it is necessary to assign a value obtained from Json to a variable? Or how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ambal245, 2020-10-24
@ambal245

First you need to parse into variables what came, and then use it. An example implementation looks like this:

$data = json_decode(file_get_contents('php://input'),true);
$text = $data['message']['text'];
$chat = $data['message']['chat']['id'];
$first_name = $data['message']['from']['first_name'];
  if ($text){
         if (strtolower($text) == "/start") $bot->reply($chat, " Здравствуй {$first_name} ");
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question