getUpdates updates telegram bot without Webhook - IT NEWS

Latest

Programming PHP tutorials for beginners. Technology and product reviews.

Monday, May 1, 2017

getUpdates updates telegram bot without Webhook

Adv

All data saved up to 24h on telegram server.
If you are using direct telegram link you will get all request as json data * Bot and Channel messages.
You will get all data from your bot.


getUpdates updates telegram bot without Webhook


Get all TELEGRAM data from your bot with PHP. 1 message /start

$token = "******";
$response = file_get_contents('https://api.telegram.org/bot$token/getUpdates');
$response = json_decode($response, true);
foreach($response['result'] as $result) {

if ($result['message'] == "") {
} else {
echo $result['message']['message_id'], ' ';
echo $result['message']['text'], '
';
}
}
1 Message ID and 2 Text


How to get only last element from Telegram getUpdates bot?

With this code you will get last element from telegram getUpdates ( Message ID and Text)

$token = "******";
$response = file_get_contents('https://api.telegram.org/bot$token/getUpdates');
$response = json_decode($response, true);
$numItems = count($response['result'])-1;
$i = 0;
foreach($response['result'] as $key=>$value) {
if(++$i === $numItems) {

if($response['result'][$numItems]['message'] == "") {
$data = "Channel post";
$msgid = $response['result'][$numItems]['channel_post']['message_id'];
$msgtext = $response['result'][$numItems]['channel_post']['text'];
}
else {
$data = "Bot post";
$msgid = $response['result'][$numItems]['message']['message_id'];
$msgtext = $response['result'][$numItems]['message']['text'];
}

}
}
echo "Last $data : ID: $msgid | Text: $msgtext";



This script not automatic. You can use CRON for script auto launch. Then you can save data from Telegram to Data base (Mysql,Sql).


https://www.facebook.com/chakabiz
https://www.youtube.com/channel/UCYuMRNb_SRZ4FMsZjnHRZUA

1 comment:

  1. HEllo trying to run this code but it fails when sending message it appears blank or this messages on php

    E_NOTICE : type 8 -- Undefined index: message -- at line 7

    E_NOTICE : type 8 -- Undefined index: message -- at line 7

    E_NOTICE : type 8 -- Undefined index: message -- at line 7

    ReplyDelete