Events 101 : Example of Sending Telegram notification on new ticket

In this tutorial we will demonstrate how to send a Telegram notification to your mobile when a new ticket is created to demonstrate capabilities of the software.

Telegram bots can be found here

Introduction

Telegram bots are pretty much similar to chatting with a normal human, except that you will be interacting with bots !
Also note that you must be the one initiating a chat

Creating a bot

We will briefly explain how to create a Telegram bot to make things easier for everyone interested in perusing developing using it.

First you have to talk to BotFather, which is also a bot that creates the bots

Once you start, type /newbot and follow the process , giving it a name and such



Once done, you will receive a token, Copy the token for later

Now using your mobile or web, Talk to the bot ( Bot will have username you specified, you can talk to it using https://telegram.me/YourBotUsername , replacing YourBotUsername with your bot's username

Once you start the chat, head to this link using your browser:
https://api.telegram.org/bot@token@/getUpdates

Replace @token@ with your token, Example using the the token in above example, link would be:
https://api.telegram.org/bot161052654:BBH1r32xy95H06p3UtZu_50xBwRQxO1up4I/getUpdates  





The highlighted will be your Chat ID

Now that we have collected all necessary information to send a message , let's setup the notification event

Creating Notification Event

Now for the last and fun part, setting up the event to receive a notification every-time someone opens a new notification

Head to app/Providers/CustomEventsServiceProvider.php , open with a text editor ( In the example i will use Sublime 3 ) , and start writing inside boot() function

First, i want this notification to be sent on Creation on a new ticket so the event appropiate would be ticket.created , So initial code :

Event::listen('ticket.created', function($id,$title,$user_id,$assigned_to,$department_id,$status){
// Do stuff 
});

Now, i want to also include Name of the user who created the ticket , so i will have to fetch the user creating the ticket using User::find(..)

After that , we will utilize SendMessage method from Telegram, so the final code would be

Event::listen('ticket.created', function($id,$title,$user_id,$assigned_to,$department_id,$status){

    $user = User::find($user_id); // Fetch the user information
    $token = "161052654:BBH1r32xy95H06p3UtZu_50xBwRQxO1up4I"; // Token from above
    $chat_id = "190088830"; // Chat ID from Above
    
    $textMessage = "User ".$user->name. " Just opened a new ticket";
    $textMessage = urlencode($textMessage);

    $link = sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s",$token,$chat_id,$textMessage);

    $client = new GuzzleHttp\Client();
    $request = $client->get($link);

});

And you're done ! , once a new ticket is created, you will receive a notification ( Go ahead and test it ) , like below: