TelegramPHP Documentation

Telegram
in package
implements EventEmitterInterface uses EventEmitterTrait, Methods

The Telegram client - the role `Discord\Discord` plays under DiscordPHP.

Owns the Http transport, the Factory, the caches ($telegram->chats, $telegram->users), and the update source: long polling by default, or a webhook listener when one is configured. Every Bot API method is a real method on this class, pulled in from the generated Methods trait, so calls read exactly as the documentation does:

$telegram = new Telegram(['token' => getenv('TELEGRAM_TOKEN')]);

$telegram->on(Event::MESSAGE, function (Message $message) use ($telegram) {
    $telegram->sendMessage($message->chat->id, 'pong');
});

$telegram->run();

Updates arrive as parts and are emitted twice: once as Event::UPDATE with the whole Update, and once under the update's own type (Event::MESSAGE, Event::CALLBACK_QUERY, ...) with just that payload, which is what most handlers want.

Tags
link
https://core.telegram.org/bots/api
author

Valithor Obsidion valithor@valgorithms.com

Table of Contents

Interfaces

EventEmitterInterface

Constants

BOT_API_VERSION  : mixed = '10.3'
The Bot API version the bundled spec was generated from.
VERSION  : mixed = '1.0.0'
REPOSITORIES  : mixed = ['chats' => \Telegram\Repository\ChatRepository...
Repository accessors: property name => class.

Properties

$chats  : ChatRepository
$users  : UserRepository
$botUser  : ?User
The bot's own account, once `getMe` has answered.
$factory  : Factory
$http  : HttpInterface
$logger  : LoggerInterface
$loop  : LoopInterface
$options  : array<string, mixed>
$poller  : ?Poller
$ready  : bool
$webhook  : ?Server
$repositories  : array<string, AbstractRepository>

Methods

__construct()  : mixed
__get()  : mixed
Lazily builds the repository caches.
__isset()  : bool
downloadFile()  : PromiseInterface<string|int, string>
Downloads a file - either a {@see File} from `getFile`, or a `file_id` this resolves first.
getBotUser()  : ?User
The bot's own account, once {@see start()} has resolved.
getFactory()  : Factory
getHttp()  : HttpInterface
getLogger()  : LoggerInterface
getLoop()  : LoopInterface
getOption()  : mixed
One client option.
getPoller()  : ?Poller
getWebhookServer()  : ?Server
handleUpdate()  : Update
Turns one raw update into parts and emits it - as {@see Event::UPDATE} with the whole {@see Update}, then under its own type with just that payload.
isReady()  : bool
request()  : PromiseInterface<string|int, mixed>
Calls any Bot API method by name, including one this build has no generated method for - a new method on a Bot API server that is ahead of the bundled spec, for instance.
run()  : void
Identifies the bot, starts receiving updates, and runs the event loop.
shutdown()  : void
Stops receiving updates and stops the event loop.
start()  : PromiseInterface<string|int, User>
Identifies the bot and starts receiving updates, without running the loop.
stop()  : void
Stops receiving updates. The loop keeps running unless you stop it too.
callApi()  : PromiseInterface<string|int, mixed>
Dispatches one Bot API call - the hook the generated {@see Methods} trait calls into.
configureOptions()  : void
Declares the options this client accepts. Subclasses add their own by calling `parent::configureOptions($resolver)` first - see {@see \Telegram\CommandClient\TelegramCommandClient}.
hydrate()  : mixed
Builds the result of a call into parts.
resolveOptions()  : array<string, mixed>
startPolling()  : void
startWebhook()  : void
cachePayload()  : void
Keeps the chats and users an update mentions in the local caches.

Constants

BOT_API_VERSION

The Bot API version the bundled spec was generated from.

public mixed BOT_API_VERSION = '10.3'

REPOSITORIES

Repository accessors: property name => class.

private mixed REPOSITORIES = ['chats' => \Telegram\Repository\ChatRepository::class, 'users' => \Telegram\Repository\UserRepository::class]

Properties

$botUser

The bot's own account, once `getMe` has answered.

protected ?User $botUser = null

$logger

protected LoggerInterface $logger

$options

protected array<string, mixed> $options

Methods

__construct()

public __construct([array<string, mixed> $options = [] ]) : mixed
Parameters
$options : array<string, mixed> = []
Tags
throws
ExceptionInterface

When the options are wrong.

__get()

Lazily builds the repository caches.

public __get(string $name) : mixed
Parameters
$name : string

__isset()

public __isset(string $name) : bool
Parameters
$name : string
Return values
bool

downloadFile()

Downloads a file - either a {@see File} from `getFile`, or a `file_id` this resolves first.

public downloadFile(File|string $file) : PromiseInterface<string|int, string>
Parameters
$file : File|string
Return values
PromiseInterface<string|int, string>

The file's bytes.

getBotUser()

The bot's own account, once {@see start()} has resolved.

public getBotUser() : ?User
Return values
?User

getLogger()

public getLogger() : LoggerInterface
Return values
LoggerInterface

getLoop()

public getLoop() : LoopInterface
Return values
LoopInterface

getOption()

One client option.

public getOption(string $name[, mixed $default = null ]) : mixed
Parameters
$name : string
$default : mixed = null

handleUpdate()

Turns one raw update into parts and emits it - as {@see Event::UPDATE} with the whole {@see Update}, then under its own type with just that payload.

public handleUpdate(Update|array<string, mixed> $raw) : Update

Both the poller and the webhook listener funnel through here, so a handler never has to care which one is in use.

Parameters
$raw : Update|array<string, mixed>

A hydrated update, or the payload a webhook delivery arrived as.

Return values
Update

isReady()

public isReady() : bool
Return values
bool

request()

Calls any Bot API method by name, including one this build has no generated method for - a new method on a Bot API server that is ahead of the bundled spec, for instance.

public request(string $method[, array<string, mixed> $payload = [] ][, array<int, string> $returns = [] ]) : PromiseInterface<string|int, mixed>
Parameters
$method : string
$payload : array<string, mixed> = []
$returns : array<int, string> = []

Telegram type tokens; empty leaves the result raw.

Return values
PromiseInterface<string|int, mixed>

run()

Identifies the bot, starts receiving updates, and runs the event loop.

public run([bool $runLoop = true ]) : void

Pass false to keep the loop under your own control - useful when the bot shares a loop with other ReactPHP services.

Parameters
$runLoop : bool = true

shutdown()

Stops receiving updates and stops the event loop.

public shutdown() : void

Named shutdown rather than close because close is a Bot API method of its own - it releases the bot from Telegram's server before moving it to a local one - and the generated method for it must keep that name.

start()

Identifies the bot and starts receiving updates, without running the loop.

public start() : PromiseInterface<string|int, User>
Return values
PromiseInterface<string|int, User>

The bot's own account.

stop()

Stops receiving updates. The loop keeps running unless you stop it too.

public stop() : void

callApi()

Dispatches one Bot API call - the hook the generated {@see Methods} trait calls into.

protected callApi(string $method, array<string, mixed> $arguments, array<int, string> $returns) : PromiseInterface<string|int, mixed>
Parameters
$method : string
$arguments : array<string, mixed>

The method's arguments, unset ones included.

$returns : array<int, string>

Telegram type tokens the result hydrates as.

Return values
PromiseInterface<string|int, mixed>

configureOptions()

Declares the options this client accepts. Subclasses add their own by calling `parent::configureOptions($resolver)` first - see {@see \Telegram\CommandClient\TelegramCommandClient}.

protected configureOptions(OptionsResolver $resolver) : void
Parameters
$resolver : OptionsResolver

hydrate()

Builds the result of a call into parts.

protected hydrate(array<int, string> $returns, mixed $result) : mixed

A few methods return one of two things - editMessageText answers with the edited Message, or with true when the message is an inline one the bot cannot read back - so the token that matches the shape actually returned wins.

Parameters
$returns : array<int, string>
$result : mixed

resolveOptions()

protected resolveOptions(array<string, mixed> $options) : array<string, mixed>
Parameters
$options : array<string, mixed>
Return values
array<string, mixed>

startPolling()

protected startPolling() : void

startWebhook()

protected startWebhook() : void

cachePayload()

Keeps the chats and users an update mentions in the local caches.

private cachePayload(mixed $payload) : void
Parameters
$payload : mixed
On this page

Search results