Telegram integration tutorial
Introduction
Section titled “Introduction”
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:
- Create a Pingstreams Project
- Create a Telegram Bot
- Configure the Webhook
- Set the Webhook endpoint for Telegram
- Subscribe to a Webhook on Pingstreams
- Test the integration
Create a Pingstreams Project
Section titled “Create a Pingstreams Project”First of all create a Pingstreams project. Log in to the Pingstreams Console, then click on “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).

Nice, your project is ready.
Create a Telegram Bot
Section titled “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.
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.

Your bot is now operative. Do you see the token? We will need it shortly.
Configure the Webhook
Section titled “Configure the Webhook”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.

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

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

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

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

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

Set the Webhook endpoint for Telegram
Section titled “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:

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:

Subscribe to a Webhook on Pingstreams
Section titled “Subscribe to a Webhook on Pingstreams”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.

Then click on “ADD SUBSCRIPTION” button.

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:

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.

Well, the Webhook is now configured!
Test the integration
Section titled “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”.

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

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

Done! The integration is completed.
Additional Examples
Section titled “Additional Examples”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!