Skip to content
FacebookYouTubeX (Twitter)

Telegram integration tutorial

Telegram Integration

The Telegram Integration for Pingstreams allows to expand the offer of customer service through the exchange of messages between Telegram and Pingstreams support. This integration allows you to connect your company’s Telegram bot to your Pingstreams 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 Pingstreams webchat, along with messages from other channels. Your agents will have a single work environment.

The following are the steps involved in our tutorial:

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

First of all create a Pingstreams project. Log in to the Pingstreams Console, then click on “Add project”.

Add Project

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).

Create Project

Nice, your project is ready.

Well, let’s move on the Telegram app, on the desktop version or on the mobile one. Search and start the conversation with @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.

BotFather

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

We use the Replit service to fast create our own NodeJS web application endpoint. Fork the tutorial application, available at this url: https://replit.com/@Pingstreams/pingstreams-telegram-tutorial-app.

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

Fork App

The app is forked and ready to run.

Now move back to your Pingstreams project’s Settings > Project Settings > General section and copy the Project Id

Copy Project ID

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

Paste Project ID

Now move back (again) to your Pingstreams 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”.

Generate Secret

That’s your shared secret key, click on “Copy”.

Copy Secret

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

Paste Secret Key

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 Token

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:

Set Webhook Endpoint

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

Move to your Pingstreams project’s Settings > Project Settings > Developer and click on the “MANAGE WEBHOOK” button.

Manage Webhook

Then click on “ADD SUBSCRIPTION” button.

Add Subscription

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 “/pingstreams” at the end of the URL, like in figure:

Subscription Settings

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 through webhook’s APIs (see here). We don’t need it for our goal, but save it in a safe place because it will only be displayed once.

Webhook Secret

Well, the Webhook is now configured!

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”.

Test Bot

A default message “/start” will be sent to Pingstreams webchat and an agent can now reply.

Webchat Message

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

Telegram Response

Done! The integration is completed.

Here you can find an example code to integrate with Twilio (WhatsApp) based on the same principles treated in this tutorial

And here, another example code, this time for Facebook Messenger integration.

If you have any problems do not hesitate to write us on our Community forum!

See you on our next tutorial!