Skip to content

CryDeTaan/laravel-telegram-bot

Repository files navigation

About

This project is a simple example of introducing a Telegram Bot into a Laravel app which can be used to send notifications to users or admins of a site.

I made use of several packages to aid in bootstrapping this as quickly as possible. The idea was to understand how to introduce said bot and not spend a lot of time developing an application...

There are two parts to this setup,

  1. Setting up the Laravel application
  2. Setting up and configuring the Telegram bot.

Getting started with Laravel

# Clone the repo
git clone https://github.com/CryDeTaan/laravel-telegram-bot.git
cd laravel-telegram-bot

# Install Laravel and Packages
composer install

# Install Frontend assets
npm install && npm run dev

# Copy .env and generate application key
cp .env.example .env
php artisan key:generate

# Configure .env with your editor, specifically update the DB section
pstorm . # I am using PHPStorm and this will open the current directory in PHPStorm. 

# Run migrations
php artisan migrate

# Serve the app and open, I am using valet
valet link && valet open

At this point an application will be up and running and can be accessed using a browser where users can register and login as well.

Welcome Page

Setting up the Telegram bot

This section has a bit more detail, it is after all about Telegram bots.

When it comes to Telegram bots, it all starts with the BotFather. Find it in the search and start a new chat with him.

Bot Father

I think the team at Telegram made it fairly easy to use, and for the most part self-explanatory. I mean, we know what we are here for, so let's create a new bot using /newbot. Start the conversation to create a bot, it's like having a conversation.

Bot Father Chat

Once the BotFather congratulates you, you will find two pieces of information that you need to capture in the .env file.

  1. The URL, you can copy it from the message from the BotFather and update the TELEGRAM_BOT_URL in this case it is https://t.me/laravel_telegram_test_bot.
  2. Update the TELEGRAM_BOT_TOKEN with the API Token, in this case 2017805637:AAFAB6wvnYC5aGzm07_hqUIVokMy82Z6WpA

The values in the .env should now look something like this:

TELEGRAM_BOT_URL=https://t.me/laravel_telegram_test_bot
TELEGRAM_BOT_TOKEN=2017805637:AAFAB6wvnYC5aGzm07_hqUIVokMy82Z6WpA

Now, the following is probably going to cause some confusion, and more so in a development environment. But it is required so that Telegram can reach the application to send a message to a webhook. As I mentioned, I am using Laravel Valet which makes it easy to make an application accessible from the internet while in a development setting, i.e. behind some sort of NAT etc. Laravel Valet is using ngrok under the hood. So if you are not using Valet to serve the application, and you do not currently have a way to expose the application in a development setting to the internet, I suggest looking at ngrok.

Anyway, we need to expose the application to the internet in order to configure the bot to make use of the application's Telegram webhook.

The Telegram Bot's Webhook URL can be set using the telegram:configure-webhook artisan command.

Artisan command

Now we can create a new user and enable their Telegram notifications. After creating a user and logging in, navigate to the profile section, find the drop-down to the top right of the page. There you'll find an option to enable Telegram notifications.

Enable Telegram Notifications

When clicking on the enable button, a new tab will open prompting you to Open Telegram.

Open Telegram prompt

Once the Open Telegram option has been selected, Telegram will open and there you can start the bot by clicking on the Start button.

Start Bot chat

Head back to the web app and select the Notification tab. Entering a message here and clicking send will deliver the message to Telegram in the chat with your bot.

Send test notification

Receive notification in Telegram

That's pretty mush it, later...

About

Telegram Bot in Laravel

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published