-
Notifications
You must be signed in to change notification settings - Fork 325
Some basic examples
As it took me several hours to get some working examples for this bot (as the example bot is totally outdated) I'd like to share my experiences here so hopefully they can help others to build their bot way faster.
try
{
$bot = new \TelegramBot\Api\Client(API_KEY);
// Tell the bot what to do like waiting for callbacks or defining some commands
$bot->run();
}
catch (\TelegramBot\Api\Exception $e)
{
$e->getMessage();
}
$bot->command('telegram', function ($message) use ($bot) {
// Tell me what I should do here like sending a message or a photo
});
$bot->sendMessage(
$message->getChat()->getId(),
"<a href=\"https://telegram.org\"</a>",
"HTML"
);
$bot->sendPhoto(
$message->getChat()->getId(),
"https://telegram.org/file/464001876/2/61q3quSkA-o.229990/0448e8588e48b3942c",
"Some <b>bold</b> caption for the photo",
null,
null,
null,
"HTML"
);
I will extend this as soon as I have some spare time
You can also use emojis in there using PHP's function html_entity_decode
$question = 'How can I help you?';
$keyboard = new \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup([
[
['text' => html_entity_decode("💙") . " Option 1", 'callback_data' => 'option1'],
['text' => html_entity_decode("💚") . " Option 2", 'callback_data' => 'option2'],
['text' => html_entity_decode("💛") . " Option 3", 'callback_data' => 'option3']
]
]);
$bot->sendMessage($message->getChat()->getId(), $question, null, false, null, $keyboard);
# This function is called if someone clicks on an inline button
$bot->callbackQuery(function ($message) use ($bot) {
if ($message->getData() == "option1")
{
// If you want you can send some kind of popup message after the user clicked one of the buttons
$bot->answerCallbackQuery($message->getId(), "You clicked on option1. Loading...");
$bot->sendMessage(
$message->getFrom()->getId(),
"Hi " . $message->getFrom()->getUsername() . ", you've choosen <b>Option 1</b>",
"HTML"
);
}
elseif ($message->getData() == "option2")
{
// If you want you can send some kind of popup message after the user clicked one of the buttons
$bot->answerCallbackQuery($message->getId(), "You clicked on option2. Loading...");
$bot->sendMessage(
$message->getFrom()->getId(),
"Hi " . $message->getFrom()->getUsername() . ", you've choosen <b>Option 2</b>",
"HTML"
);
}
elseif ($message->getData() == "option3")
{
// If you want you can send some kind of popup message after the user clicked one of the buttons
$bot->answerCallbackQuery($message->getId(), "You clicked on option3. Loading...");
$bot->sendMessage(
$message->getFrom()->getId(),
"Hi " . $message->getFrom()->getUsername() . ", you've choosen <b>Option 3</b>",
"HTML"
);
}
});
Send message
$temp_message = $bot->sendMessage(
$message->getFrom()->getId(),
"Asking my database, one moment ... 🎲",
"HTML"
);
And edit it's content afterwards
$bot->deleteMessage(
$temp_message->getChat()->getId(),
$temp_message->getMessageId()
);
Send message
$temp_message = $bot->sendMessage(
$message->getFrom()->getId(),
"Asking my database, one moment ... 🎲",
"HTML"
);
And delete it afterwards
$bot->deleteMessage(
$temp_message->getChat()->getId(),
$temp_message->getMessageId(),
);
$bot->command('letmetype', function ($message) use ($bot) {
# Send "Typing..."
$bot->sendChatAction(
$message->getChat()->getId(),
'typing'
);
});
Sometimes if you are stuck with this library dumping some variables helps bringing some light into the darkness 😄 So with the following snipped you get the raw body the Telegram API sends after someone e.g. clicked on an inline button:
# This function is called if someone clicks on an inline button
$bot->callbackQuery(function ($message) use ($bot) {
$rawBody = $bot->getRawBody();
$bot->sendMessage(
$message->getFrom()->getId(),
$rawBody
);
}
The answer looks like this:
{"update_id":27417224,
"callback_query":{"id":"987654321098765432","from":{"id":45645645,"is_bot":false,"first_name":"ME","username":"me","language_code":"de"},"message":{"message_id":924,"from":{"id":1231231231,"is_bot":true,"first_name":"someones","username":"someones_bot"},"chat":{"id":45645645,"first_name":"ME","username":"me","type":"private"},"date":1594544189,"text":"How can I help you?","reply_markup":{"inline_keyboard":[[{"text":"Option 1","callback_data":"option1"},{"text":"Option 2","callback_data":"option2"},{"text":"Option 3","callback_data":"option3"}]]}},"chat_instance":"-1234567890123456789","data":"option1"}}
Another way to dump some object or array can be the following:
# This function is called if someone clicks on an inline button
$bot->callbackQuery(function ($message) use ($bot) {
ob_start();
var_dump($message);
$result = ob_get_contents();
ob_flush();
$bot->sendMessage(
$message->getFrom()->getId(),
$result
);
}
This results in the following:
object(TelegramBot\Api\Types\CallbackQuery)#56 (7) {
["id":protected]=>
string(18) "987654321098765432"
["from":protected]=>
object(TelegramBot\Api\Types\User)#57 (6) {
["id":protected]=>
int(45645645)
["firstName":protected]=>
string(9) "ME"
["lastName":protected]=>
NULL
["username":protected]=>
string(9) "me"
["languageCode":protected]=>
string(2) "de"
["isBot":protected]=>
bool(false)
}
["message":protected]=>
object(TelegramBot\Api\Types\Message)#58 (45) {
["messageId":protected]=>
int(924)
["from":protected]=>
object(TelegramBot\Api\Types\User)#59 (6) {
["id":protected]=>
int(1231231231)
["firstName":protected]=>
string(13) "someones"
["lastName":protected]=>
NULL
["username":protected]=>
string(17) "someones_bot"
["languageCode":protected]=>
NULL
["isBot":protected]=>
bool(true)
}
["date":protected]=>
int(1594544189)
["chat":protected]=>
object(TelegramBot\Api\Types\Chat)#60 (13) {
["id":protected]=>
int(45645645)
["type":protected]=>
string(7) "private"
["title":protected]=>
NULL
["username":protected]=>
string(9) "me"
["firstName":protected]=>
string(9) "ME"
["lastName":protected]=>
NULL
["allMembersAreAdministrators":protected]=>
NULL
["photo":protected]=>
NULL
["description":protected]=>
NULL
["inviteLink":protected]=>
NULL
["pinnedMessage":protected]=>
NULL
["stickerSetName":protected]=>
NULL
["canSetStickerSet":protected]=>
NULL
}
["forwardFrom":protected]=>
NULL
["forwardFromChat":protected]=>
NULL
["forwardFromMessageId":protected]=>
NULL
["forwardSignature":protected]=>
NULL
["forwardSenderName":protected]=>
NULL
["forwardDate":protected]=>
NULL
["replyToMessage":protected]=>
NULL
["editDate":protected]=>
NULL
["mediaGroupId":protected]=>
NULL
["authorSignature":protected]=>
NULL
["text":protected]=>
string(22) "How can I help you?"
["entities":protected]=>
NULL
["captionEntities":protected]=>
NULL
["audio":protected]=>
NULL
["document":protected]=>
NULL
["animation":protected]=>
NULL
["photo":protected]=>
NULL
["sticker":protected]=>
NULL
["video":protected]=>
NULL
["voice":protected]=>
NULL
["caption":protected]=>
NULL
["contact":protected]=>
NULL
["location":protected]=>
NULL
["venue":protected]=>
NULL
["poll":protected]=>
NULL
["dice":protected]=>
NULL
["newChatMembers":protected]=>
NULL
["leftChatMember":protected]=>
NULL
["newChatTitle":protected]=>
NULL
["newChatPhoto":protected]=>
NULL
["deleteChatPhoto":protected]=>
NULL
["groupChatCreated":protected]=>
NULL
["supergroupChatCreated":protected]=>
NULL
["channelChatCreated":protected]=>
NULL
["migrateToChatId":protected]=>
NULL
["migrateFromChatId":protected]=>
NULL
["pinnedMessage":protected]=>
NULL
["invoice":protected]=>
NULL
["successfulPayment":protected]=>
NULL
["connectedWebsite":protected]=>
NULL
["replyMarkup":protected]=>
NULL
}
["inlineMessageId":protected]=>
NULL
["chatInstance":protected]=>
string(20) "-1234567890123456789"
["data":protected]=>
string(11) "option1"
["gameShortName":protected]=>
NULL
}
I will extend this Wiki as soon as I've figured out some more examples.
Stay tuned or collaborate if you want to!