Skip to content

Commit

Permalink
Added parsing methods; updated README.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabnicolas committed Feb 22, 2018
1 parent 49f8e92 commit 8db9a12
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,27 @@ $telegram_bot->sendPhoto($image_path, $chat_id, function() use ($image_path){

### Get incoming messages from users to Telegram bot
Be careful to not expose this code as an API for users; keep user privacy in mind.

The easiest way is:
```php
$result = $telegram_bot->getParsedUpdates();
if($result){
foreach($result as $key=>$message){
// Analyze each $message as an array (update_id, message_id, from_id, from_username, date, text).
}
}else{
// No new messages incoming.
}
```
Check https://core.telegram.org/bots/api#getting-updates , get_updates.php and TelegramBot class for more details about the structure and the underlying APIs.

In particular, `get_updates.php` in the web server takes care of handling new messages and saving them into a database table; in future it will be possible to retrieve saved messages (And delete them to mark them as read) thanks to the API wrapper.



To retrieve more details and handle data manually:
```php
// For some parameters details/usage, check get_updates.php and https://core.telegram.org/bots/api#getting-updates.
// For some parameters details/usage, check get_updates.php, TelegramBot class and .
$update_id=0; // Update ID; clients send 0 the first time, then uses last possible number.
$offset=0; // Optional, default 0; used to acknowledge messages. Read Telegram API for details.
$limit=100; // Optional, default 100; used to limit the number of updates handled at the same time.
Expand Down
39 changes: 11 additions & 28 deletions get_updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,18 @@
$update_id = post_parameter('update_id');
$update_id = $update_id!=null ? $update_id : 0;

$result = json_decode($telegram_bot->getUpdates($update_id));
if($result->ok){
if(array_key_exists('result', $result)){
$updates=$result->result;
$fetched_updates=array();
foreach($updates as $key=>$update){
$message=$update->message;

// Query params
$query_params=array();
$query_params['update_id']=$update->update_id;
$query_params['message_id']=$message->message_id;
$query_params['from_id']=$message->from->id;
$query_params['from_username']=$message->from->username;
$query_params['date']=$message->date;
$query_params['text']=$message->text;

// Insert values into database
$statement = $db->getPDO()->prepare(
"INSERT INTO updates (update_id, message_id, from_id, from_username, date, text)
VALUES (:update_id, :message_id, :from_id, :from_username, :date, :text)");
$statement->execute($query_params);

$fetched_updates[]=$query_params;
}
json_echo(array('status'=>0, 'message'=>$fetched_updates)); // At moment just for testing - Will be replaced.
}else{
json_echo(array('status'=>1, 'message'=>'No updates pending.'));
$result = $telegram_bot->getParsedUpdates();
if($result){
foreach($result as $key=>$message_params){
// Insert values into database
$statement = $db->getPDO()->prepare(
"INSERT INTO updates (update_id, message_id, from_id, from_username, date, text)
VALUES (:update_id, :message_id, :from_id, :from_username, :date, :text)");
$statement->execute($message_params);
}
json_echo(array('status'=>0, 'message'=>$result)); // At moment just for testing - Will be replaced.
}else{
json_echo(array('status'=>1, 'message'=>'No updates pending.'));
}

$success=true;
Expand Down
35 changes: 35 additions & 0 deletions lib/telegrambot.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,40 @@ function sendPhoto($image_url, $chat_id, $callback=null){
function getUpdates($offset=0,$limit=100,$timeout=0){
return $this->callTelegramAPI("getUpdates",['offset'=>$offset,'limit'=>$limit,'timeout'=>$timeout]);
}

function parseUpdates($updates,$chat_id=null){
$result = json_decode($updates);
if($result->ok){
if(array_key_exists('result', $result)){
$updates=$result->result;
$parsed_update=array();
foreach($updates as $key=>$update){
$message=$update->message;

// Reconstruct user message in a more human readable way with only necessary infos
$parsed_message=array();
$parsed_message['update_id']=$update->update_id;
$parsed_message['message_id']=$message->message_id;
$parsed_message['from_id']=$message->from->id;
$parsed_message['from_username']=$message->from->username;
$parsed_message['date']=$message->date;
$parsed_message['text']=$message->text;

if($chat_id!=null && $chat_id==$parsed_message['from_id']){
// As PHP is not strongly typed, we can just return straight away a single element.
return $parsed_message;
}else{
// Save the parsed message into a list.
$parsed_update[]=$parsed_message;
}
}
return $parsed_update;
}else return false;
}else return false;
}

function getParsedUpdates($chat_id=null,$offset=0,$limit=100,$timeout=0){
return $this->parseUpdates($this->getUpdates($offset,$limit,$timeout),$chat_id);
}
}
?>

0 comments on commit 8db9a12

Please sign in to comment.