-1

I want make input message in telegram bot using php language I want something like :

When bot say "send your code" The user send something like "AC6J7A" How I can get the code I mean how to make message handler .

<?php
$token = 'XXXX';
define('API_KEY',$token);
function bot($method,$datas=[]){
  $url = "https://api.telegram.org/bot".API_KEY."/".$method;
  $datas = http_build_query($datas);
  $res = file_get_contents($url.'?'.$datas);
  return json_decode($res);
}

 bot('sendMessage',[
     'chat_id' => $chat_id,
     'text' => 'Send Your Code : ']);
?>

How can I get the next message of user or can i make message handler ?

1 Answers1

-1

Do this:

<?php

$token = '   TOKEN   ';
$website = 'https://api.telegram.org/bot'.$token;
$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);
$message = $update['message']['text'];
$id = $update['message']['from']['id'];
$name = $update['message']['from']['first_name'];
$surname = $update['message']['from']['last_name'];
$username = $update['message']['from']['username'];

function sendMessage($id, $text, $button = null){
    GLOBAL $token;
    switch(isset($button)){
        case true:
            $H = '&reply_markup={"inline_keyboard":['.$button.']}';
            $url = 'https://api.telegram.org/bot'.$token."/sendMessage?chat_id=$id&parse_mode=HTML&text=".urlencode($text).$H;
            break;
        default:
            $url = 'https://api.telegram.org/bot'.$token."/sendMessage?chat_id=$id&parse_mode=HTML&text=".urlencode($text);
            break;
    }
    file_get_contents($url);
}

switch($message){
    case '/start':
        $botmessage = 'send your code'; ## if you want this to work you MUST define as "$botmessage" every message the bot send to the user
        sendMessage($id, $botmessage);
        break;
}

$read = file(__DIR__."/$id.txt");
switch($read[0]){
    case 'send your code':
        $codeoftheuser = $read[0];
        ## Code to be executed when the user gives you the code
        break;
}

switch($message){
    case false:  ## This has to be done to make it working in case you use callback query too
        break;
    default: ## This is to write down the last message your bot sent
        $myfile = fopen("$id.txt", 'w+');
        fwrite($myfile, $botmessage);
        fclose($myfile);
        break;
}

?>
M1001
  • 144
  • 1
  • 11