Blog Detail

03

Aug
Interact Fluently with Telegram Bots via Laravel Telegraph cover image

arrow_back Interact Fluently with Telegram Bots via Laravel Telegraph

Telegraph is a Laravel package for fluently interacting with Telegram Bots made by def:studio

Telegraph::message('hello world')
    ->keyboard(Keyboard::make()->buttons([
            Button::make('Delete')->action('delete')->param('id', '42'),
            Button::make('open')->url('https://test.it'),
    ]))->send();

Installation

You can install the package via composer:

composer require defstudio/telegraph

Publish and launch required migrations:

php artisan vendor:publish --tag="telegraph-migrations"

php artisan migrate

Optionally, you can publish the config file with:

php artisan vendor:publish --tag="telegraph-config"

Creating a new Telegram bot

  • Go to the @BotFather app on Telegram.
  • Send /newbot, to start creating a new Bot and setting its name and username.
  • Take note of the bot token.
  • Allow the bot to join Telegram groups:
  • Now you need to choose how much the bot will be able to read from the chats. Send /setjoingroups command to @BotFather, and select your bot privacy:
  • enable: to handle only / commands handling
  • disable: to allow the bot to read all messages sent to the chat

Registering a bot with Telegraph

Any number of bots can be created, both programmatically and through an artisan command

You can add a new bot issuing the dedicated artisan command:

php artisan telegraph:new-bot

If you are implementing a custom bot creation logic, you can create a new bot using the TelegraphBot model:

$bot = TelegraphBot::create([
    'token' => $token,
    'name' => $name,
]);

Setting a webhook for the bot

A webhook lets your bot answer commands issued from telegram chats and buttons inside messages

php artisan telegraph:set-webhook {bot_id}

the bot_id argument is mandatory if you have created more than one bot

A webhook can be created programmatically for a bot by calling its registerWebhook() method

/** @var TelegraphBot $bot */
$bot->registerWebhook()->send();

Adding a chat to a Telegraph Bot

Associating one or more chats to a bot, it is enabled to send messages to that chat and interact with commands

php artisan telegraph:new-chat {bot_id}

the bot_id argument is mandatory if you have created more than one bot

Sending a message

After a bot and at least one chat have been set up, this package can be used to post new messages:

use DefStudio\Telegraph\Models\TelegraphChat;

/** @var TelegraphChat $chat */

$chat->html("<strong>Hello!</strong>\n\nI'm here!")->send();

Here’s the basic introduction of this package but this package has a lot more features and options. If you want to dig more, you can visit its complete documentation here.

Published at : 03-08-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project