Skip to content

Commit

Permalink
Add Custom Keyboard Markup Functionality and Usage Instructions.
Browse files Browse the repository at this point in the history
  • Loading branch information
irazasyed committed Jun 30, 2015
1 parent 7bf3996 commit fb95347
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 31 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,51 @@ To remove webhook (if it was set before).
$response = $telegram->removeWebhook();
```

### Use Custom Keyboard

#### ReplyKeyboardMarkup

The below example will send a message and automatically show a custom keyboard.
Since we passed third parameter as true, the keyboard will be shown only once.

See [ReplyKeyboardMarkup](https://core.telegram.org/bots/api#replykeyboardmarkup) docs for a list of supported parameters and other info.

Example:

```php
$reply_markup = $telegram->replyKeyboardMarkup([
['YES', 'NO', 'IDK'],
['COOL', 'LOL'],
['Button'],
['Last Button']
], true, true);
$response = $telegram->sendMessage('CHAT_ID', 'Hello World', false, null, $reply_markup);
$messageId = $response->getMessageId();
```

#### ReplyKeyboardHide

Telegram clients will hide the current custom keyboard and display the default letter-keyboard.
See [ReplyKeyboardHide](https://core.telegram.org/bots/api#replykeyboardhide) docs for more info.

Supported Parameter: boolean `$selective` defaults to false.
```php
$reply_markup = $telegram->replyKeyboardHide();
$response = $telegram->sendMessage('CHAT_ID', 'Hello World', false, null, $reply_markup);
```

#### ForceReply

Shows reply interface to the user, as if they manually selected the bot‘s message and tapped ’Reply'.
See [ForceReply](https://core.telegram.org/bots/api#forcereply) docs for more info.

Supported Parameter: boolean `$selective` defaults to false.

```php
$reply_markup = $telegram->forceReply();
$response = $telegram->sendMessage('CHAT_ID', 'Hello World', false, null, $reply_markup);
```

## Asynchronous Requests (Non-Blocking)

By default, sending an API request to Telegram Bot API will be a synchronous request, and block the execution of the script until it gets a response from the server or timeouts after 60 secs (throwing a Guzzle exception).
Expand Down
114 changes: 83 additions & 31 deletions src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Irazasyed\Telegram;

use Irazasyed\Telegram\Objects\KeyboardMarkup;
use Irazasyed\Telegram\Objects\User;
use Irazasyed\Telegram\Objects\Update;
use Irazasyed\Telegram\Objects\Message;
Expand Down Expand Up @@ -177,11 +178,11 @@ public function getMe()
*
* @link https://core.telegram.org/bots/api#sendmessage
*
* @param int $chat_id
* @param string $text
* @param bool $disable_web_page_preview
* @param int $reply_to_message_id
* @param ReplyKeyboardMarkup|ReplyKeyboardHide|ForceReply $reply_markup
* @param int $chat_id
* @param string $text
* @param bool $disable_web_page_preview
* @param int $reply_to_message_id
* @param KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\Message
*/
Expand Down Expand Up @@ -222,11 +223,11 @@ public function forwardMessage($chat_id, $from_chat_id, $message_id)
*
* @link https://core.telegram.org/bots/api#sendphoto
*
* @param int $chat_id
* @param string $photo
* @param string $caption
* @param int $reply_to_message_id
* @param ReplyKeyboardMarkup|ReplyKeyboardHide|ForceReply $reply_markup
* @param int $chat_id
* @param string $photo
* @param string $caption
* @param int $reply_to_message_id
* @param KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\Message
*/
Expand All @@ -242,10 +243,10 @@ public function sendPhoto($chat_id, $photo, $caption = null, $reply_to_message_i
*
* @link https://core.telegram.org/bots/api#sendaudio
*
* @param int $chat_id
* @param string $audio
* @param int $reply_to_message_id
* @param ReplyKeyboardMarkup|ReplyKeyboardHide|ForceReply $reply_markup
* @param int $chat_id
* @param string $audio
* @param int $reply_to_message_id
* @param KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\Message
*/
Expand All @@ -261,10 +262,10 @@ public function sendAudio($chat_id, $audio, $reply_to_message_id = null, $reply_
*
* @link https://core.telegram.org/bots/api#senddocument
*
* @param int $chat_id
* @param string $document
* @param int $reply_to_message_id
* @param ReplyKeyboardMarkup|ReplyKeyboardHide|ForceReply $reply_markup
* @param int $chat_id
* @param string $document
* @param int $reply_to_message_id
* @param KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\Message
*/
Expand All @@ -280,10 +281,10 @@ public function sendDocument($chat_id, $document, $reply_to_message_id = null, $
*
* @link https://core.telegram.org/bots/api#sendsticker
*
* @param int $chat_id
* @param string $sticker
* @param int $reply_to_message_id
* @param ReplyKeyboardMarkup|ReplyKeyboardHide|ForceReply $reply_markup
* @param int $chat_id
* @param string $sticker
* @param int $reply_to_message_id
* @param KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\Message
*
Expand All @@ -306,10 +307,10 @@ public function sendSticker($chat_id, $sticker, $reply_to_message_id = null, $re
* @see sendDocument
* @link https://core.telegram.org/bots/api#sendvideo
*
* @param int $chat_id
* @param string $video
* @param int $reply_to_message_id
* @param ReplyKeyboardMarkup|ReplyKeyboardHide|ForceReply $reply_markup
* @param int $chat_id
* @param string $video
* @param int $reply_to_message_id
* @param KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\Message
*/
Expand All @@ -325,11 +326,11 @@ public function sendVideo($chat_id, $video, $reply_to_message_id = null, $reply_
*
* @link https://core.telegram.org/bots/api#sendlocation
*
* @param int $chat_id
* @param float $latitude
* @param float $longitude
* @param int $reply_to_message_id
* @param ReplyKeyboardMarkup|ReplyKeyboardHide|ForceReply $reply_markup
* @param int $chat_id
* @param float $latitude
* @param float $longitude
* @param int $reply_to_message_id
* @param KeyboardMarkup $reply_markup
*
* @return \Irazasyed\Telegram\Objects\Message
*/
Expand Down Expand Up @@ -449,6 +450,57 @@ public function getUpdates($offset = null, $limit = null, $timeout = null)
return $updates;
}

/**
* Builds a custom keyboard markup.
*
* @link https://core.telegram.org/bots/api#replykeyboardmarkup
*
* @param array $keyboard
* @param bool $resize_keyboard
* @param bool $one_time_keyboard
* @param bool $selective
*
* @return string
*/
public function replyKeyboardMarkup(
$keyboard,
$resize_keyboard = false,
$one_time_keyboard = false,
$selective = false
) {
return json_encode(compact('keyboard', 'resize_keyboard', 'one_time_keyboard', 'selective'));
}

/**
* Hide the current custom keyboard and display the default letter-keyboard.
*
* @link https://core.telegram.org/bots/api#replykeyboardhide
*
* @param bool $selective
*
* @return string
*/
public static function replyKeyboardHide($selective = false)
{
$hide_keyboard = true;
return json_encode(compact('hide_keyboard', 'selective'));
}

/**
* Display a reply interface to the user (act as if the user has selected the bot‘s message and tapped ’Reply').
*
* @link https://core.telegram.org/bots/api#forcereply
*
* @param bool $selective
*
* @return string
*/
public static function forceReply($selective = false)
{
$force_reply = true;
return json_encode(compact('force_reply', 'selective'));
}

/**
* Sends a GET request to Telegram Bot API and returns the result.
*
Expand Down

0 comments on commit fb95347

Please sign in to comment.