Retrieve chats & messages
Learn how to retrieve chats, groups and messages on Telegram using Unipile API.
Get individual and group chats
All chats are grouped into a single inbox. To fetch them, use the List all Chats method.
You can apply filters such as archive status, unread status, or chat type (individual or group).
const { data, error } = await messagingApi.getChatsList({
path: {
account_id: "acc_123456789",
},
query: {
limit: 20,
},
});chats = messaging_api.get_chats_list(
"acc_123456789",
limit=20,
)To get a single chat, use Get a Chat.
const { data, error } = await messagingApi.getChat({
path: {
account_id: "acc_123456789",
chat_id: "chat_id",
},
});chat = messaging_api.get_chat("chat_id", "acc_123456789")Download user profile pictures
A Telegram User may expose a private_picture_download_url instead of a public_picture_url. The private URL points to the authenticated Get a User Picture endpoint; it is not a public image URL.
Request the URL with your API key in the X-API-KEY header to download the image:
const { data: picture } = await usersApi.getUserPicture({
path: {
account_id: "acc_123456789",
user_id: "user_id",
},
});picture = users_api.get_user_picture(
"user_id",
"acc_123456789",
)curl --request GET \
--url 'https://api.unipile.com/v2/acc_123456789/users/user_id/picture' \
--header 'X-API-KEY: your-api-key' \
--output profile-pictureDo not use private_picture_download_url directly as an image source in a browser: the request would be unauthenticated, and exposing an API key in client-side code is unsafe. Fetch the picture from your backend instead. See Private download URLs for the general behavior.
Get Messages
To list messages of a Chat, use List all Chat Messages method and provide the chat ID.
const { data, error } = await messagingApi.getMessagesList({
path: {
account_id: "acc_123456789",
chat_id: "chat_id",
},
query: {
limit: 20,
},
});messages = messaging_api.get_messages_list(
"chat_id",
"acc_123456789",
limit=20,
)curl --request GET \
--url https://api.unipile.com/v2/account_id/chats/chat_id/messages \
--header 'accept: application/json'To get a single Message, use Get a Message method and provide the chat ID with the message ID.
const { data, error } = await messagingApi.getMessage({
path: {
account_id: "acc_123456789",
chat_id: "chat_id",
message_id: "message_id",
},
});message = messaging_api.get_message(
"chat_id",
"message_id",
"acc_123456789",
)curl --request GET \
--url https://api.unipile.com/v2/account_id/chats/chat_id/messages/message_id \
--header 'accept: application/json'Be notified about new messages
To receive new upserted messages, setup a Webhook that listen for message.new events.
You'll be notified about any received message, but also any sent messages. This can be useful for your application to be aware of messages sent by the account owner from other devices. To ignore those messages, filter events on the is_sender value of the Message object.
Updated 1 day ago