DEF STUDIO srl

TelegraphBot

Bot informations are stored in database inside a telegraph_bots table and can be retrieved using DefStudio\Telegraph\Models\TelegaphBot model or using a custom Bot model.

Custom Bot Model

To customize on your own Bot model, make sure that your custom model extends the DefStudio\Telegraph\Models\TelegraphBot, e.g. App\Models\Bot, it will looks like this:

<?php

namespace App\Models;

use DefStudio\Telegraph\Models\TelegraphBot as BaseModel;

class Bot extends BaseModel
{

}

You should specify the class name of your model in the models.bot key of the telegraph config file.

'models' => [
    'bot' => App\Models\Bot::class,
],

Available methods

url()

retrieves the bot url

/** @var \DefStudio\Telegraph\Models\TelegraphBot $telegraphBot */

$telegraphBot->url();

// https://t.me/my-bot-name

info()

retrieves the bot information from Telegraph APIs

/** @var \DefStudio\Telegraph\Models\TelegraphBot $telegraphBot */

$telegraphBot->info();


/*
id: xxxxx
is_bot: true
first_name: telegraph-test
username: my_test_bot
can_join_groups: true
can_read_all_group_messages: false
supports_inline_queries: false
*/

registerCommands()

register commands in Telegram Bot in order to display them to the user when the "/" key is pressed

/** @var \DefStudio\Telegraph\Models\TelegraphBot $telegraphBot */

$telegraphBot->registerCommands([
    'command1' => 'command 1 description',
    'command2' => 'command 2 description'
])->send();

unregisterCommands()

resets Telegram Bot registered commands

/** @var \DefStudio\Telegraph\Models\TelegraphBot $telegraphBot */

$telegraphBot->unregisterCommands()->send();

registerWebhook()

register a webhook url

/** @var \DefStudio\Telegraph\Models\TelegraphBot $telegraphBot */

$telegraphBot->registerWebhook()->send();

you can use the method parameters to customize the webhook settings:

  • dropPendingUpdates: drops pending updates from telegram
  • maxConnections: maximum allowed simultaneous connections to the webhook (defaults to 40)
  • secretToken: secret token to be sent in a X-Telegram-Bot-Api-Secret-Token header to verify the authenticity of the webhook

unregisterWebhook()

unregister a webhook url

/** @var \DefStudio\Telegraph\Models\TelegraphBot $telegraphBot */

$telegraphBot->unregisterWebhook()->send();

getWebhookDebugInfo()

retrieves webhook debug data

/** @var \DefStudio\Telegraph\Models\TelegraphBot $telegraphBot */

$telegraphBot->getWebhookDebugInfo()->send();

replyWebhook()

replies to a webhook callback

/** @var \DefStudio\Telegraph\Models\TelegraphBot $telegraphBot */

$telegraphBot->replyWebhook($callbackQueryId, 'message received', $showAlert)->send();

updates()

Retrieves the Bot message and callback query updates using manual polling

/** @var \DefStudio\Telegraph\Models\TelegraphBot $telegraphBot */

$telegraphBot->updates()->each(function(TelegramUpdate $update){
    // ...
});
Warning

Manual updates polling is not available if a webhook is set up for the bot. Webhook should be remove first using its unregisterWebhook method

Long Polling

In production environment, a timeout (in seconds) should be declared, in order to allow long polling:

$telegraphBot->updates(timeout: 60)->each(function(TelegramUpdate $update){
    // ...
});

store()

Downloads a media file and stores it in the given path

/** @var DefStudio\Telegraph\DTO\Photo $photo */

$telegraphBot->store($photo, Storage::path('bot/images'), 'The Photo.jpg');
Warning

There is an hard limit of 20BM for downloaded files sizes. This is a Telegram limitation for bots, sadly, there is no workaround available. refer the docs for more info.

getFileInfo()

Retrieve file info from ID

$telegraphBot->getFileInfo($fileId)->send();

answerInlineQuery()

send back the results for an inline query

 $telegraphBot->answerInlineQuery($inlineQuery->id(), [
    InlineQueryResultPhoto::make($logo->id."-light", "https://logofinder.dev/$logo->id/light.jpg", "https://logofinder.dev/$logo->id/light/thumb.jpg")
        ->caption('Light Logo'),
    InlineQueryResultPhoto::make($logo->id."-dark", "https://logofinder.dev/$logo->id/dark.jpg", "https://logofinder.dev/$logo->id/dark/thumb.jpg")
        ->caption('Light Logo'),
])->cache(seconds: 600)->send();

setBaseUrl()

allows to override Telegram API url on a per-message basis:

$telegraphBot->setBaseUrl('https://my-secret-server.dev');
Warning

Follow installation instructions for creating the database tables

Suggest a change
Last updated 03 October 2024