1
AddMessageHandler
dax edited this page 2019-05-22 10:48:37 +02:00
Ogni metodo anonimo passato come argomento a AddMessageHandler() viene aggiunto in una coda. Tale collezione di metodi viene iterata ad ogni messaggio in arrivo ed ciascun metodo viene eseguito, uno per volta. Riferendosi a $message all'interno del metodo anonimo si ottiene il messaggio ricevuto in quel momento (stdClass) ed è quindi possibile manipolarlo, $bot da accesso alla libreria con i soliti metodi.
<?php
#
# include class
#
require('Telegram.class.php');
#
# instance
#
$bot = new TelegramBot('mytoken');
#
# optional - if not used, the bot accept messages from all ids
#
$bot->PermitFromId(362870431);
#
# add action
#
$bot->AddMessageHandler(function($message) use(&$bot) {
if( $message->IsText )
{
if( TelegramBot::Contains( $message->text, 'foto') )
{
$bot->SendImage( $message->chat->id, 'daxtech.png' );
}else {
$bot->SendMessage( $message->chat->id, "$message->text, what?" );
}
}
});
#
# start bot messages handling
#
$bot->Listen();
?>
stdClass $message ha la seguente struttura:
[from] => Array
(
[id] => 613696670
[is_bot] => 1
[first_name] => DaxTech
[username] => DaxTechBot
)
[chat] => Array
(
[id] => 362870431
[first_name] => Dax
[username] => daxtech
[type] => private
)
[date] => 1534972231
[text] => Ciao Brott!!!, what?
[IsText] => 1
[IsDocument] =>
[IsPhoto] =>
[IsVoice] =>