<?php include 'basics.php'; use unreal4u\TelegramAPI\TgLog; use unreal4u\TelegramAPI\Telegram\Methods\SendLocation; $tgLog = new TgLog(BOT_TOKEN); $location = new SendLocation(); $location->chat_id = A_USER_CHAT_ID; $location->latitude = 43.296482; $location->longitude = 5.369763; $message = $tgLog->performApiRequest($location); echo '<pre>'; var_dump($message); echo '</pre>';
<?php include 'basics.php'; use unreal4u\TelegramAPI\Telegram\Methods\getWebhookInfo; use unreal4u\TelegramAPI\TgLog; $tgLog = new TgLog(BOT_TOKEN); $webHookInfo = new getWebhookInfo(); $hookInfo = $tgLog->performApiRequest($webHookInfo); echo '<pre>'; var_dump($hookInfo); echo '</pre>';
protected function sendToUser(SendMessage $sendMessage) : TelegramTypes { $this->logger->debug('Sending the message to user'); $tgLog = new TgLog($this->token, $this->logger, $this->HTTPClient); return $tgLog->performApiRequest($sendMessage); }
<?php include 'basics.php'; use unreal4u\TelegramAPI\Telegram\Types\Custom\InputFile; use unreal4u\TelegramAPI\TgLog; use unreal4u\TelegramAPI\Telegram\Methods\SendVideo; use GuzzleHttp\Exception\ClientException; $tgLog = new TgLog(BOT_TOKEN); $sendVideo = new SendVideo(); $sendVideo->chat_id = A_USER_CHAT_ID; $sendVideo->video = new InputFile('examples/binary-test-data/demo-video.mp4'); $sendVideo->caption = 'Example of a video file sent with Telegram'; try { $message = $tgLog->performApiRequest($sendVideo); echo '<pre>'; var_dump($message); echo '</pre>'; } catch (ClientException $e) { echo '<pre>'; var_dump($e->getMessage()); var_dump($e->getRequest()); var_dump($e->getTrace()); echo '</pre>'; }
<?php use GuzzleHttp\Exception\ClientException; use unreal4u\TelegramAPI\Telegram\Methods\GetUserProfilePhotos; use unreal4u\TelegramAPI\TgLog; include 'basics.php'; $userProfilePhotos = new GetUserProfilePhotos(); $tgLog = new TgLog(BOT_TOKEN); $userProfilePhotos->user_id = A_USER_ID; try { $userProfilePhotos = $tgLog->performApiRequest($userProfilePhotos); echo '<pre>'; var_dump($userProfilePhotos); echo '</pre>'; } catch (ClientException $e) { echo 'Error detected trying to get user profile photos, original response: <pre>'; print_r((string) $e->getResponse()->getBody()); echo '</pre>'; die; }
<?php include 'basics.php'; use unreal4u\TelegramAPI\Telegram\Types\Custom\InputFile; use unreal4u\TelegramAPI\TgLog; use unreal4u\TelegramAPI\Telegram\Methods\SendVoice; use GuzzleHttp\Exception\ClientException; $tgLog = new TgLog(BOT_TOKEN); $sendVoice = new SendVoice(); $sendVoice->chat_id = A_USER_CHAT_ID; $sendVoice->voice = new InputFile('examples/binary-test-data/demo-voice.ogg'); try { $message = $tgLog->performApiRequest($sendVoice); echo '<pre>'; var_dump($message); echo '</pre>'; } catch (ClientException $e) { echo '<pre>'; var_dump($e->getMessage()); var_dump($e->getRequest()); var_dump($e->getTrace()); echo '</pre>'; }
<?php include 'basics.php'; use unreal4u\TelegramAPI\Telegram\Types\Custom\InputFile; use unreal4u\TelegramAPI\TgLog; use unreal4u\TelegramAPI\Telegram\Methods\SendAudio; use GuzzleHttp\Exception\ClientException; $tgLog = new TgLog(BOT_TOKEN); $sendAudio = new SendAudio(); $sendAudio->chat_id = A_USER_CHAT_ID; $sendAudio->audio = new InputFile('examples/binary-test-data/ICQ-uh-oh.mp3'); $sendAudio->title = 'The famous ICQ new message alert'; try { $message = $tgLog->performApiRequest($sendAudio); echo '<pre>'; var_dump($message); echo '</pre>'; } catch (ClientException $e) { echo '<pre>'; var_dump($e->getMessage()); var_dump($e->getRequest()); var_dump($e->getTrace()); echo '</pre>'; }
<?php include 'basics.php'; use Monolog\Logger; use Monolog\Handler\StreamHandler; use unreal4u\TelegramAPI\Telegram\Methods\EditMessageText; use unreal4u\TelegramAPI\Telegram\Methods\SendMessage; use unreal4u\TelegramAPI\TgLog; $logger = new Logger('CUSTOM-EXAMPLE'); $logger->pushHandler(new StreamHandler('logs/custom-example.log')); $tgLog = new TgLog(BOT_TOKEN, $logger); $sendMessage = new SendMessage(); $sendMessage->chat_id = A_GROUP_CHAT_ID; $sendMessage->text = 'Hello world, this is a test'; $message = $tgLog->performApiRequest($sendMessage); $editMessageText = new EditMessageText(); $editMessageText->message_id = $message->message_id; #$editMessageText->message_id = 1112222; $editMessageText->chat_id = $message->chat->id; $editMessageText->text = 'Sorry, this is the correction of an hello world'; try { $message = $tgLog->performApiRequest($editMessageText); } catch (ClientException $e) { var_dump($e->getRequest()); }
/** * @FIXME this function name will probably change in the future! * This will download a file and offer it to the user */ private function downloadSticker(Message $message) : unreal4uBot { $this->logger->debug(sprintf('Got a sticker request, downloading sticker')); $sendMessage = new SendMessage(); $sendMessage->chat_id = $message->chat->id; try { $getFile = new GetFile(); $getFile->file_id = $message->sticker->file_id; $tgLog = new TgLog($this->token, $this->logger); $file = $tgLog->performApiRequest($getFile); $tgDocument = $tgLog->downloadFile($file); $this->logger->debug('Downloaded sticker, sending it to temporary directory'); file_put_contents(sprintf('media/%s', basename($file->file_path)), (string) $tgDocument); #$image = \imagecreatefromwebp(sprintf('media/%s', basename($file->file_path))); #\imagepng($image, 'media/'.basename($file->file_path).'.png'); #\imagedestroy($image); $sendMessage->text = sprintf('Download link for sticker: http://media.unreal4u.com/%s', basename($file->file_path)); } catch (\Exception $e) { $this->logger->error('Problem downloading sticker: ' . $e->getMessage()); $sendMessage->text = sprintf('There was a problem downloading your sticker, please retry later'); } try { $tgLog->performApiRequest($sendMessage); $this->logger->debug('Sent message to user'); } catch (\Exception $e) { $this->logger->warning('Caught exception while trying to send message to user: ' . $e->getMessage()); } return $this; }
<?php include 'basics.php'; use GuzzleHttp\Exception\ClientException; use unreal4u\TelegramAPI\TgLog; use unreal4u\TelegramAPI\Telegram\Methods\GetFile; $tgLog = new TgLog(BOT_TOKEN); $getFile = new GetFile(); // You can get a file id from an update, webhook or any other message object $getFile->file_id = A_FILE_ID; try { $file = $tgLog->performApiRequest($getFile); } catch (ClientException $e) { // Do whatever you want, function below contains exact JSON output from Telegram echo '<pre>'; print_r(json_decode((string) $e->getResponse()->getBody())); echo '</pre>'; } // Don't do anything if we have already output (as we can't modify those headers) if (!headers_sent()) { $tgDocument = $tgLog->downloadFile($file); header(sprintf('Content-Type: %s', $tgDocument->mime_type)); header(sprintf('Content-Length: %d', $tgDocument->file_size)); header(sprintf('Content-Disposition: inline; filename="%s"', basename($file->file_path))); print $tgDocument; }
<?php include 'basics.php'; use unreal4u\TelegramAPI\Telegram\Types\Custom\InputFile; use unreal4u\TelegramAPI\TgLog; use unreal4u\TelegramAPI\Telegram\Methods\SendPhoto; use GuzzleHttp\Exception\ClientException; $tgLog = new TgLog(BOT_TOKEN); $sendPhoto = new SendPhoto(); $sendPhoto->chat_id = A_USER_CHAT_ID; $sendPhoto->photo = new InputFile('examples/binary-test-data/demo-photo.jpg'); $sendPhoto->caption = 'Not sure if sending image or image not arriving'; try { $message = $tgLog->performApiRequest($sendPhoto); echo '<pre>'; var_dump($message); echo '</pre>'; } catch (ClientException $e) { echo '<pre>'; var_dump($e->getMessage()); var_dump($e->getRequest()); var_dump($e->getTrace()); echo '</pre>'; }
<?php include 'basics.php'; use unreal4u\TelegramAPI\TgLog; use unreal4u\TelegramAPI\Telegram\Methods\GetUpdates; $tgLog = new TgLog(BOT_TOKEN); $getUpdates = new GetUpdates(); #$getUpdates->offset = 328221148; $updates = $tgLog->performApiRequest($getUpdates); echo '<pre>'; foreach ($updates->traverseObject() as $update) { var_dump($update); #var_dump(sprintf('Chat id is #%d', $update->message->chat->id)); } echo '</pre>';
<?php include 'basics.php'; use GuzzleHttp\Exception\ClientException; use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup; use unreal4u\TelegramAPI\TgLog; use unreal4u\TelegramAPI\Telegram\Methods\SendMessage; $tgLog = new TgLog(BOT_TOKEN); $sendMessage = new SendMessage(); $sendMessage->chat_id = A_USER_CHAT_ID; $sendMessage->text = 'Hello world to the user... from a specialized getMessage file. Have you read this message?'; $sendMessage->reply_markup = new ReplyKeyboardMarkup(); $sendMessage->reply_markup->keyboard = [['Yes', 'No']]; try { $tgLog->performApiRequest($sendMessage); printf('Message "%s" sent!<br/>%s', $sendMessage->text, PHP_EOL); } catch (ClientException $e) { echo '<pre>'; var_dump($e->getRequest()); echo '</pre>'; die; }
<?php include 'basics.php'; use unreal4u\TelegramAPI\TgLog; // @TODO Change to use \unreal4u\Telegram\Methods\{GetMe, SendMessage}; once PSR-12 is definitive use unreal4u\TelegramAPI\Telegram\Methods\GetMe; use unreal4u\TelegramAPI\Telegram\Methods\SendMessage; $tgLog = new TgLog(BOT_TOKEN); $getMe = new GetMe(); $userInformation = $tgLog->performApiRequest($getMe); printf('This bot is called <strong>%s</strong> (username %s) and has id <strong>%d</strong>%s', $userInformation->first_name, $userInformation->username, $userInformation->id, PHP_EOL); $sendMessage = new SendMessage(); $sendMessage->chat_id = A_USER_CHAT_ID; $sendMessage->text = 'Hello world to the user... now revamped!'; $tgLog->performApiRequest($sendMessage); $sendMessage = new SendMessage(); $sendMessage->chat_id = A_GROUP_CHAT_ID; $sendMessage->text = 'And this is an hello the the group... from scratch :D'; $tgLog->performApiRequest($sendMessage);
<?php include 'basics.php'; use unreal4u\TelegramAPI\Telegram\Methods\SendSticker; use unreal4u\TelegramAPI\TgLog; use GuzzleHttp\Exception\ClientException; $tgLog = new TgLog(BOT_TOKEN); $sendSticker = new SendSticker(); $sendSticker->chat_id = A_USER_CHAT_ID; // Send out an existing sticker $sendSticker->sticker = 'BQADBAADsgUAApv7sgABW0IQT2B3WekC'; try { $message = $tgLog->performApiRequest($sendSticker); echo '<pre>'; var_dump($message); echo '</pre>'; } catch (ClientException $e) { echo '<pre>'; var_dump($e->getMessage()); var_dump($e->getRequest()); var_dump($e->getTrace()); echo '</pre>'; }
<?php include 'basics.php'; use GuzzleHttp\Exception\ClientException; use unreal4u\TelegramAPI\Telegram\Methods\GetChatMember; use unreal4u\TelegramAPI\TgLog; $tgLog = new TgLog(BOT_TOKEN); $getCM = new GetChatMember(); $getCM->chat_id = A_GROUP_CHAT_ID; $getCM->user_id = A_USER_CHAT_ID; try { $response = $tgLog->performApiRequest($getCM); echo '<pre>'; var_dump($response); echo '</pre>'; } catch (ClientException $e) { // Do whatever you want, function below contains exact JSON output from Telegram echo 'Exception catched, error is: <pre>'; print_r(json_decode((string) $e->getResponse()->getBody())); echo '</pre>'; }