Skip to content

Some basic examples

Christian edited this page Jul 11, 2020 · 6 revisions

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.

Initialize your bot

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

Define some commands

$bot->command('telegram', function ($message) use ($bot) {
	// Tell me what I should do here like sending a message or a photo
});

Commands

Send message

$bot->sendMessage(
	$message->getChat()->getId(),
	"<a href=\"https://telegram.org\"</a>",
	"HTML"
);

Send photo

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

More to come

I will extend this as soon as I have some spare time

Inline keyboard

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("&#128153;") . " Option 1", 'callback_data' => 'option1'],
		['text' => html_entity_decode("&#128154;") . " Option 2", 'callback_data' => 'option2'],
		['text' => html_entity_decode("&#128155;") . " Option 3", 'callback_data' => 'option3']
	]
]);

$bot->sendMessage($message->getChat()->getId(), $question, null, false, null, $keyboard);

callbackQuery (Wait for someone clicking on an inline button)

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

I will extend this Wiki as soon as I've figured out some more examples.

Stay tuned or collaborate if you want to!

Clone this wiki locally