Telegram
Learn how to list chats of a Telegram account and send a message in one of them using Unipile SDK.
Prerequisites
Before we start, you need to:
-
Create an Application in the dashboard.
-
Create an API Key from the dashboard.
-
(Optional) Install the SDK & Setup Client
Get the Unipile SDK.npm i @unipile/sdkpnpm add unipileCreate a new file that setup the Unipile Client using your API Key generated previously generated.
import { UnipileMessaging } from "unipile"; const key = "apikey"; const messagingApi = new UnipileMessaging({ key });import unipile configuration = unipile.Configuration() configuration.api_key["apiKey"] = "apikey" api_client = unipile.ApiClient(configuration) messaging_api = unipile.MessagingApi(api_client)
1. Link an account
In the Accounts section of the Dashboard, click on Link account.
Select Telegram and authenticate to the account you want to show chats for your demo application.
Once the account is linked, keep the given ID as it will be used later.
2. Try to call API Methods
Now, all you have to do is calling the method you want and specify the account ID given in Step 1. In this quickstart guide, we'll see how to list chats and send a message in one of them.
List chats
To get the chats list of the account acc_1234567890, use List all Chats :
const chats = await messagingApi.getChatsList({
path: {
account_id: "acc_1234567890",
},
});chats = messaging_api.get_chats_list("acc_1234567890")curl --request GET \
--url https://api.unipile.com/v2/acc_1234567890/chats \
--header 'X-API-KEY: api-key' \
--header 'accept: application/json'Send a message in an existing chat
To send a message in an existing chat, use Send a Message and specify the ID of the chat you want to send the message in.
The chat_id can be found in the previously retrieved list.
await messagingApi.sendMessage({
path: {
account_id: "acc_1234567890",
chat_id: "-2112ZA1231",
},
body: {
text: "Hello from Unipile!",
},
});messaging_api.send_message(
"-2112ZA1231",
"acc_1234567890",
{
"text": "Hello from Unipile!",
},
)curl --request POST \
--url https://api.unipile.com/v2/acc_1234567890/chats/-2112ZA1231/messages/send \
--header 'X-API-KEY: api-key' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"text": "Hello from Unipile!"
}
'
Learn once, integrate everything !You can reuse the same Methods with accounts of other providers that support Messaging features !
3. Discover Unipile's fundamentals
Updated 14 days ago