musthafa
musthafa

Reputation: 433

Telegram BOT Api is not working while sending commands

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??

Upvotes: 1

Views: 3343

Answers (3)

Alexey Shablowski
Alexey Shablowski

Reputation: 71

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);
}

Upvotes: 0

Majid Salehi
Majid Salehi

Reputation: 16

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);

Upvotes: 0

Regwy
Regwy

Reputation: 121

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);
}

Upvotes: 0

Related Questions