skip to Main Content

I am new to telegram BOT Api, i just set webhook , when i manually surf my web link, My Bot is receiving messages, but when i send commands or any text from telegram bot i am not getting any reply from the server

I didnt get any problem while setting the webhook, my certificate is self signed , i also uploaded the self signed certificate while setting the webhook, because telegram says to upload self signed (.PEM) certificate , but still i am not receiving any message while i send commands or any text from Telegram BOT but Manual surf is working

Here is my sample code

<?php 
ini_set('error_reporting', E_ALL);
$botToken = "MY_TOKEN";
$website ="https://api.telegram.org/bot".$botToken;

$update=file_get_contents("php://input");
$update = json_decode($content, TRUE);

$chatID = $update["message"]["chat"]["id"];
$message =$update["message"]["text"];

switch ($message) {
    case "/test":
        sendMessage($chatID, "test");
        break;
    case "/cancel":
        sendMessage($chatID, "cancel");
        break;

    default:
        sendMessage($chatID, "default");
        break;
}

function sendMessage($chat_id, $msg){
    $url = $GLOBALS["website"]."/sendMessage?chat_id=".$chat_id."&text=".urlencode($msg);
    file_get_contents($url);
}

please help me that where i am making my mistake??

3

Answers


  1. In sendMessage() function you’ve written into the url sendMessage, try instead sendmessage (lowcase)

    function sendMessage($chat_id, $msg){
        $url = $GLOBALS["website"]."/sendMessage?chat_id=".$chat_id."&text=".urlencode($msg);
        file_get_contents($url);
    }
    
    Login or Signup to reply.
  2. just here must change ::

    before :

    $update=file_get_contents("php://input");
    $update = json_decode($content, TRUE);
    

    after :

    $update=file_get_contents("php://input");
    $update = json_decode($update, TRUE);
    
    Login or Signup to reply.
  3. This code is work

    <?php 
    ini_set('error_reporting', E_ALL);
    $botToken = "MY_TOKEN";
    $website ="https://api.telegram.org/bot".$botToken;
    
    //$update=file_get_contents("php://input");
    $content = file_get_contents("php://input"); <-----
    $update = json_decode($content, TRUE);
    
    $chatID = $update["message"]["chat"]["id"];
    $message =$update["message"]["text"];
    
    switch ($message) {
        case "/test":
            sendMessage($chatID, "test");
            break;
        case "/cancel":
            sendMessage($chatID, "cancel");
            break;
    
        default:
            sendMessage($chatID, "default");
            break;
    }
    
    function sendMessage($chat_id, $msg){
        $url = $GLOBALS["website"]."/sendMessage?chat_id=".$chat_id."&text=".urlencode($msg);
        file_get_contents($url);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search