# Telegram integration tutorial

## Introduction

![](https://user-images.githubusercontent.com/45603238/175036688-f26d0efe-3150-425b-bab2-6ca07b63adf6.png)

The Telegram Integration for Tiledesk allows to expand the offer of customer service through the exchange of messages between Telegram and Tiledesk support. This integration allows you to connect your company's Telegram bot to your Tiledesk account, thus creating a tunnel between the two platforms. Therefore customers can reach your support simply by writing to your Telegram bot and these messages will be delivered to the Tiledesk webchat, along with messages from other channels. Your agents will have a single work environment.

## Steps

The following are the steps involved in our tutorial:

1. Create a Tiledesk Project
2. Create a Telegram Bot
3. Configure the Webhook
4. Set the Webhook endpoint for Telegram
5. Subscribe to a Webhook on Tiledesk
6. Test the integration

## Create a Tiledesk Project

First of all create a Tiledesk project. Log in to the [Tiledesk Console](https://panel.tiledesk.com/), then click on "Add project".

![Create a new project](https://user-images.githubusercontent.com/45603238/174244101-5d096a58-857d-45e2-9311-bca7c05063bf.png)

Now choose a name for your project (i.e. Telegram Tutorial) and click the "CREATE PROJECT" button (leave all the options on their default values).

![Choose the project name](https://user-images.githubusercontent.com/45603238/175033347-d45427f5-6654-420e-a1da-f77fba15ab1b.png)

Nice, your project is ready.

## Create a Telegram Bot

Well, let's move on the Telegram app, on the desktop version or on the mobile one. Search and start the conversation with [@BotFather](https://t.me/botfather).

Now type _/newbot_ to create a new bot or choose from the options proposed by the bot.

So, choose a name for your bot and a username also, following the instruction given by @BotFather.

![New telegram bot](https://user-images.githubusercontent.com/45603238/174252867-0c159500-2937-4a50-9993-d11226fbc9af.png)

Your bot is now operative. Do you see the token? We will need it shortly.

## Configure the Webhook

We use the [Replit](https://replit.com/~) service to fast create our own NodeJS web application endpoint. Fork the tutorial application, available at this url: [https://replit.com/@GiovanniTroisi/tiledesk-telegram-tutorial-app](https://replit.com/@GiovanniTroisi/tiledesk-telegram-tutorial-app#index.js).

Use the fork button and choose a name for your app.

![Fork Replit project](https://user-images.githubusercontent.com/45603238/174968675-5813f440-a39f-4af8-8a3d-10596e117e98.png)

The app is forked and ready to run.

Now move back to your Tiledesk project's _Settings_ > _Project Settings_ > _General_ section and copy the **Project Id**

![Tiledesk Project Id](https://user-images.githubusercontent.com/45603238/174268648-d3f28bab-67aa-4683-93d3-8de33e97a324.png)

Paste the Project Id in the _index.js_ file of your NodeJS app on Replit, as shown in figure:

![Paste Project Id](https://user-images.githubusercontent.com/45603238/174282279-1c069dc4-02cf-4597-9002-74c4f7650440.png)

Now move back (again) to your Tiledesk project's _Settings_ > _Project Settings_ > _Developer_ section. We will generate a new **secret key** that will be used to sign your JWT token. Press "GENERATE SHARED SECRET" button, but note that every time you generate a new secret the previous one will be no longer valid and you have to replace wherever you used it. Then click on "GENERATE".

![Shared Secret](https://user-images.githubusercontent.com/45603238/174272766-a397b947-2cca-4f5d-99b1-53efe6ff96b9.png)

That's your shared secret key, click on "Copy".

![Copy Shared Secret](https://user-images.githubusercontent.com/45603238/174273202-104b99fe-c790-4081-b60a-d843b9554eca.png)

Paste the Secret Key in the _index.js_ file of your NodeJS app on Replit, as shown in figure:

![Paste Shared Secret](https://user-images.githubusercontent.com/45603238/174282285-f2720cc6-91f6-4f51-9974-18140edb3df8.png)

## Set the Webhook endpoint for Telegram

Now it's time to use the token provided by @BotFather earlier. Copy and paste it in the _index.js_ file of your NodeJS app on Replit, as shown in figure:

![Paste Telegram token](https://user-images.githubusercontent.com/45603238/175038765-04233d8b-a05f-452c-95f5-4dc8c6f2bda1.png)

In the same file we need to set also the webhook endpoint for Telegram, that is the endpoint related to your webhook to which Telegram will send messages.

Click on "Run" button to start the server, then copy the URL in the red circle in _index.js_, inside the function **setWebhookEndpoint()**, adding "/telegram" at the end of the URL, like in figure:

![Endpoint Url](https://user-images.githubusercontent.com/45603238/174815348-e1dfb890-aec4-4640-acb8-100a9f545935.png)

## Subscribe to a Webhook on Tiledesk

We almost done, the last thing to do is to add a subscription for Tiledesk. Let's go!

Move to your Tiledesk project's _Settings_ > _Project Settings_ > _Developer_ and click on the "MANAGE WEBHOOK" button.

![Manage Webhook Button](https://user-images.githubusercontent.com/45603238/174817082-e01011ae-8977-42e8-a10d-53dd21b1e5c5.png)

Then click on "ADD SUBSCRIPTION" button.

![Add subscription](https://user-images.githubusercontent.com/45603238/174286884-5a229142-cef0-4be8-8e5f-418b3568586e.png)

In the **New Subscription** popup select the **Message Create (only for Telegram channel)** option from the dropdown and type the Webhook Target that is the same URL than before, adding "/tiledesk" at the end of the URL, like in figure:

![Create subscription configuration](https://user-images.githubusercontent.com/45603238/174843423-062a80c0-dcb5-4235-bf28-aeec74cdc675.png)

Now click on "CREATE SUBSCRIPTION" button.

What you will see next will be the secret associated with your webhook. Keep the secret to interact with webhook trough webhook’s APIs (see [here](https://developer.tiledesk.com/apis/webhooks/subscriptions)). We don't need it for our goal, but save it in a safe place because it will only be displayed once.

![Webhook secret](https://user-images.githubusercontent.com/45603238/174982652-773fa46c-a3f2-4cf6-b571-bb0d484468fb.png)

**Well, the Webhook is now configured!**

## Test the integration

Finally we can test the Telegram integration. Move to Telegram app and search the bot created later (search it for name or username or just click on the link present on @BotFather). Then click on "Start".

![Start conversation on Telegram](https://user-images.githubusercontent.com/45603238/174973179-d006755f-7bb6-46e9-bec1-50f6bfcede43.png)

A default message "/start" will be sent to Tiledesk webchat and an agent can now reply.

![Tiledesk chat](https://user-images.githubusercontent.com/45603238/175004664-93cc2b04-2d48-4c43-ac54-d54d929528d6.png)

Message sent from Tiledesk will be delivered in the Telegram Bot chat, like any other conversation.

![Telegram chat](https://user-images.githubusercontent.com/45603238/175005794-baa995b3-c150-45a6-ba20-adbcd50a3ba8.png)

Done! The integration is completed.

[Here](https://replit.com/@andrealeo83/tiledesk-whatsapp-twilio-app) you can find an example code to integrate with **Twilio (WhatsApp)** based on the same principles treated in this tutorial

And [here](https://replit.com/@andrealeo83/tiledesk-facebook-app#index.js), another one example code, this time for **Facebook Messenger integration**.

If you have any problems do not esitate to write us on our [Community forum](https://tiledesk.discourse.group/)!

See you on our next tutorial!

Do you have suggestions on this article? Please send us your feedback writing an email to **info@tiledesk.com**
