Send Telegram Messenger notifications on Laravel

Tram Ho

Telegram is certainly no stranger to dev brothers like WhatsApp brothers, Facebook Messenger in terms of features as well as great security capabilities. We can use Telegram to send messages to channels or groups through the Telegram Bot API . It is completely free so we can use this to report application errors, report applications daily.

You can refer to the official API Bots documentation at.

In this tutorial, we will build a simple application that allows sending messages to a channel. Let’s start working now …

Install Telegram Bot API

Telegram Bots is developed very strongly with a community of members participating in building and developing support on many different languages ​​and platforms. We can see it at: . We will be using the Telegram Bot API PHP SDK package developed by Syed Irfaq R. The reason for using it is that it supports Laravel and is highly appreciated by the community.

Install with the following cmd:

Next, add the following line to the Service Provider in config/app.php .

add to the array aliases with:

As the final step, we will publish the configuration:

The above command will create config file telegram.php in config directory. Configuration information will be set here.

Create a Telegram Bot

First we need to install the Telegram app and do an interesting thing by talking to BotFather to create a new telegram bot. ^^! quite new.

To do this, we need to send a message to @BotFather and perform a few simple steps like this:

  1. Click Start and proceed to communicate with the botfather with /start
  2. Create a new bot with /newbot
  3. Set a name and username for the bot.

Finally, you will receive confirmation that the bot has been successfully created with the token to be used for the HTTP API . We can also refer here ( ) to see what we can teach the bot.

Next, we will configure the environment variable for our bot with the token we just received

Create a channel to send notifications

Create a channel

Now, we need to create a new channel to send the notifications and set it to public so that we can easily find and join.

Next, we will invite the newly created bot with Admin privileges to allow the bot to send messages to the channel.

Configure bot

We need to configure the bot info at config/telegram.php with some token values, certificate_path and webhook_url . This information we can completely set at env :

Get Channel chat_id

To be able to send messages or notify to any channel or group we need chat_id . chat_id is a unique identifier with which we can send messages in Telegram.

Finding chat_id information we can get by creating a route so Telegram can return all information related to the bot’s activity. The returned information contains the channel and associated chat_id number that this bot owns.

Check bot activity

First we create TelegramController with updatedActivity method to capture information returned from Telegram.

add the route at routes/web.php .

Run the app to check activity with endpoint /updated-activity . Now we can get a blank message if no activity has occurred.

We will try to open the channel and issue a Hi , Hello message and then refresh the browser to check the changed information:

Easily find an id in the array channel_post/chat/id , in this case we would be chat_id of -1001120496444 . Set the TELEGRAM_CHANNEL_ID in the .env file to this value.

Create a messaging app

In this application, we will create a contact form to send contact requests as follows:

Create view

create file contactform.blade.php :

set the routes:

Set up the action

Update TeletgramController:

After successfully validating the data, we will send a message to the channel with the Telegram::sendMessage method with HTML parse_mode to perform html parse with the above text.

Finally, access the endpoint /contact to try sending the contact form to check the results.



Thus, we have been sent by Telegram bot and sent messages to channel successfully with Telegram Bot API. Hopefully after this article you can develop many practical applications for your own from Telegram such as applying to report work results or safety warning applications. Good luck!

See at:

Share the news now

Source : Viblo