Skip to content

Commit

Permalink
Add location methods
Browse files Browse the repository at this point in the history
  • Loading branch information
punyflash committed Sep 13, 2020
1 parent de1065e commit 85ec87e
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 33 deletions.
72 changes: 39 additions & 33 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use GuzzleHttp\Promise\PromiseInterface;
use WeStacks\TeleBot\Interfaces\UpdateHandler;
use WeStacks\TeleBot\Methods\DeleteWebhookMethod;
use WeStacks\TeleBot\Methods\EditMessageLiveLocationMethod;
use WeStacks\TeleBot\Methods\ForwardMessageMethod;
use WeStacks\TeleBot\Methods\GetUpdatesMethod;
use WeStacks\TeleBot\Methods\GetWebhookInfoMethod;
Expand All @@ -25,28 +26,31 @@
use WeStacks\TeleBot\Methods\SendVideoNoteMethod;
use WeStacks\TeleBot\Methods\SendVoiceMethod;
use WeStacks\TeleBot\Methods\SetWebhookMethod;
use WeStacks\TeleBot\Methods\StopMessageLiveLocationMethod;
use WeStacks\TeleBot\Objects\Update;
use WeStacks\TeleBot\Objects\WebhookInfo;

/**
* This class represents a bot instance. This is basicaly main controller for sending your Telegram requests.
*
* @method True|PromiseInterface|False deleteWebhook() Use this method to remove webhook integration if you decide to switch back to getUpdates. Returns True on success. Requires no parameters.
* @method Message|PromiseInterface|False forwardMessage(array $parameters = []) Use this method to forward messages of any kind. On success, the sent Message is returned.
* @method User|PromiseInterface|False getMe() A simple method for testing your bot's auth token. Requires no parameters. Returns basic information about the bot in form of a User object.
* @method Update[]|PromiseInterface|False getUpdates(array $parameters = []) Use this method to send photos. On success, the sent Message is returned.
* @method WebhookInfo|PromiseInterface|False getWebhookInfo() Use this method to get current webhook status. Requires no parameters. On success, returns a WebhookInfo object. If the bot is using getUpdates, will return an object with the url field empty.
* @method Message|PromiseInterface|False sendAnimation(array $parameters = []) Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound). On success, the sent Message is returned. Bots can currently send animation files of up to 50 MB in size, this limit may be changed in the future.
* @method Message|PromiseInterface|False sendAudio(array $parameters = []) Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .MP3 or .M4A format. On success, the sent Message is returned. Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future. For sending voice messages, use the sendVoice method instead.
* @method Message|PromiseInterface|False sendDocument(array $parameters = []) Use this method to send general files. On success, the sent Message is returned. Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.
* @method Message|PromiseInterface|False sendLocation(array $parameters = []) Use this method to send point on the map. On success, the sent Message is returned.
* @method Message|PromiseInterface|False sendMediaGroup(array $parameters = []) Use this method to send a group of photos or videos as an album. On success, an array of the sent Messages is returned.
* @method Message|PromiseInterface|False sendMessage(array $parameters = []) Use this method to send text messages. On success, the sent Message is returned.
* @method Message|PromiseInterface|False sendPhoto(array $parameters = []) Use this method to send photos. On success, the sent Message is returned.
* @method Message|PromiseInterface|False sendVideo(array $parameters = []) Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). On success, the sent Message is returned. Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.
* @method Message|PromiseInterface|False sendVideoNote(array $parameters = []) As of v.4.0, Telegram clients support rounded square mp4 videos of up to 1 minute long. Use this method to send video messages. On success, the sent Message is returned.
* @method Message|PromiseInterface|False sendVoice(array $parameters = []) Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .OGG file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
* @method True|PromiseInterface|False setWebhook(array $parameters = []) Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. Returns True on success.
* @method True|PromiseInterface|False deleteWebhook() Use this method to remove webhook integration if you decide to switch back to getUpdates. Returns True on success. Requires no parameters.
* @method Message|PromiseInterface|False editMessageLiveLocation() Use this method to edit live location messages. A location can be edited until its live_period expires or editing is explicitly disabled by a call to stopMessageLiveLocation. On success, if the edited message was sent by the bot, the edited Message is returned, otherwise True is returned.
* @method Message|PromiseInterface|False forwardMessage(array $parameters = []) Use this method to forward messages of any kind. On success, the sent Message is returned.
* @method User|PromiseInterface|False getMe() A simple method for testing your bot's auth token. Requires no parameters. Returns basic information about the bot in form of a User object.
* @method Update[]|PromiseInterface|False getUpdates(array $parameters = []) Use this method to send photos. On success, the sent Message is returned.
* @method WebhookInfo|PromiseInterface|False getWebhookInfo() Use this method to get current webhook status. Requires no parameters. On success, returns a WebhookInfo object. If the bot is using getUpdates, will return an object with the url field empty.
* @method Message|PromiseInterface|False sendAnimation(array $parameters = []) Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound). On success, the sent Message is returned. Bots can currently send animation files of up to 50 MB in size, this limit may be changed in the future.
* @method Message|PromiseInterface|False sendAudio(array $parameters = []) Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .MP3 or .M4A format. On success, the sent Message is returned. Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future. For sending voice messages, use the sendVoice method instead.
* @method Message|PromiseInterface|False sendDocument(array $parameters = []) Use this method to send general files. On success, the sent Message is returned. Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.
* @method Message|PromiseInterface|False sendLocation(array $parameters = []) Use this method to send point on the map. On success, the sent Message is returned.
* @method Message|PromiseInterface|False sendMediaGroup(array $parameters = []) Use this method to send a group of photos or videos as an album. On success, an array of the sent Messages is returned.
* @method Message|PromiseInterface|False sendMessage(array $parameters = []) Use this method to send text messages. On success, the sent Message is returned.
* @method Message|PromiseInterface|False sendPhoto(array $parameters = []) Use this method to send photos. On success, the sent Message is returned.
* @method Message|PromiseInterface|False sendVideo(array $parameters = []) Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). On success, the sent Message is returned. Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.
* @method Message|PromiseInterface|False sendVideoNote(array $parameters = []) As of v.4.0, Telegram clients support rounded square mp4 videos of up to 1 minute long. Use this method to send video messages. On success, the sent Message is returned.
* @method Message|PromiseInterface|False sendVoice(array $parameters = []) Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .OGG file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
* @method True|PromiseInterface|False setWebhook(array $parameters = []) Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. Returns True on success.
* @method Message|PromiseInterface|False stopMessageLiveLocation(array $parameters = []) Use this method to stop updating a live location message before live_period expires. On success, if the message was sent by the bot, the sent Message is returned, otherwise True is returned.
*
* @package WeStacks\TeleBot
*/
Expand Down Expand Up @@ -193,23 +197,25 @@ private function validUpdate(&$update)
protected function methods()
{
return [
'deleteWebhook' => DeleteWebhookMethod::class,
'forwardMessage' => ForwardMessageMethod::class,
'getMe' => GetMeMethod::class,
'getUpdates' => GetUpdatesMethod::class,
'getWebhookInfo' => GetWebhookInfoMethod::class,
'sendAnimation' => SendAnimationMethod::class,
'sendAudio' => SendAudioMethod::class,
'sendDocument' => SendDocumentMethod::class,
'sendLocation' => SendLocationMethod::class,
'sendMediaGroup' => SendMediaGroupMethod::class,
'sendMessage' => SendMessageMethod::class,
'sendPhoto' => SendPhotoMethod::class,
'sendVideo' => SendVideoMethod::class,
'sendVideoNote' => SendVideoNoteMethod::class,
'sendVoice' => SendVoiceMethod::class,
'setWebhook' => SetWebhookMethod::class,
// TODO: editMessageLiveLocation method
'deleteWebhook' => DeleteWebhookMethod::class,
'editMessageLiveLocation' => EditMessageLiveLocationMethod::class,
'forwardMessage' => ForwardMessageMethod::class,
'getMe' => GetMeMethod::class,
'getUpdates' => GetUpdatesMethod::class,
'getWebhookInfo' => GetWebhookInfoMethod::class,
'sendAnimation' => SendAnimationMethod::class,
'sendAudio' => SendAudioMethod::class,
'sendDocument' => SendDocumentMethod::class,
'sendLocation' => SendLocationMethod::class,
'sendMediaGroup' => SendMediaGroupMethod::class,
'sendMessage' => SendMessageMethod::class,
'sendPhoto' => SendPhotoMethod::class,
'sendVideo' => SendVideoMethod::class,
'sendVideoNote' => SendVideoNoteMethod::class,
'sendVoice' => SendVoiceMethod::class,
'setWebhook' => SetWebhookMethod::class,
'stopMessageLiveLocation' => StopMessageLiveLocationMethod::class,
// TODO: sendVenue method
];
}
}
36 changes: 36 additions & 0 deletions src/Methods/EditMessageLiveLocationMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace WeStacks\TeleBot\Methods;

use WeStacks\TeleBot\Helpers\TypeCaster;
use WeStacks\TeleBot\Interfaces\TelegramMethod;
use WeStacks\TeleBot\Objects\Keyboard\InlineKeyboardMarkup;
use WeStacks\TeleBot\Objects\Message;

class EditMessageLiveLocationMethod extends TelegramMethod
{
protected function request()
{
return [
'type' => 'POST',
'url' => "https://api.telegram.org/bot{$this->token}/editMessageLiveLocation",
'send' => $this->send(),
'expect' => Message::class
];
}

private function send()
{
$parameters = [
'chat_id' => 'string',
'message_id' => 'integer',
'inline_message_id' => 'string',
'latitude' => 'float',
'longitude' => 'float',
'reply_markup' => InlineKeyboardMarkup::class
];

$object = TypeCaster::castValues($this->arguments[0] ?? [], $parameters);
return [ 'json' => TypeCaster::stripArrays($object) ];
}
}
34 changes: 34 additions & 0 deletions src/Methods/StopMessageLiveLocationMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace WeStacks\TeleBot\Methods;

use WeStacks\TeleBot\Helpers\TypeCaster;
use WeStacks\TeleBot\Interfaces\TelegramMethod;
use WeStacks\TeleBot\Objects\Keyboard\InlineKeyboardMarkup;
use WeStacks\TeleBot\Objects\Message;

class StopMessageLiveLocationMethod extends TelegramMethod
{
protected function request()
{
return [
'type' => 'POST',
'url' => "https://api.telegram.org/bot{$this->token}/stopMessageLiveLocation",
'send' => $this->send(),
'expect' => Message::class
];
}

private function send()
{
$parameters = [
'chat_id' => 'string',
'message_id' => 'integer',
'inline_message_id' => 'string',
'reply_markup' => InlineKeyboardMarkup::class
];

$object = TypeCaster::castValues($this->arguments[0] ?? [], $parameters);
return [ 'json' => TypeCaster::stripArrays($object) ];
}
}
15 changes: 15 additions & 0 deletions tests/Feature/SendMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,24 @@ public function testSendLocation()
{
$message = $this->bot->sendLocation([
'chat_id' => getenv('TELEGRAM_USER_ID'),
'live_period' => 60,
'latitude' => 50.450157,
'longitude' => 30.524191
]);
$this->assertInstanceOf(Message::class, $message);

$message = $this->bot->editMessageLiveLocation([
'chat_id' => getenv('TELEGRAM_USER_ID'),
'message_id' => $message->message_id,
'latitude' => 50.451157,
'longitude' => 30.525191
]);
$this->assertInstanceOf(Message::class, $message);

$message = $this->bot->stopMessageLiveLocation([
'chat_id' => getenv('TELEGRAM_USER_ID'),
'message_id' => $message->message_id
]);
$this->assertInstanceOf(Message::class, $message);
}
}

0 comments on commit 85ec87e

Please sign in to comment.