How to interact with Telegram API
Asked Answered
O

5

65

I'm really confused as I'm trying to use Telegram's APIs after reading a lot of the documentation on http://core.telegram.org.

I have registered my app and got a hash_id and all of that stuff. But I'm not sure where to begin with.

I had worked with Spotify's API before, and was able to interact with it using http://api.spotify.com/v1/method?params:values form.

I can't find the URL for Telegram's API. I also searched a lot on the internet but couldn't find any useful examples.

Does anyone know anything about getting started to work with Telegram's API? Any help would be appreciated.

Oleson answered 5/7, 2015 at 8:10 Comment(5)
did you use Spotify's API and Telegram's API in C#?Kwabena
Used Spotify's API (through URI and JSON response) in C# and built a Spotify demo player app. Never touched Telegram's API though.Oleson
@RamtinSoltani Did you find any solution?Waldos
because tg api is about low level networking and cryptographyMerodach
It's 2023 and i have the same question, i can't even find the URL for Telegram's API on documentation.Executor
H
37

If you really want to understand Telegram API development from scratch. My advice would be to follow the steps here

https://core.telegram.org/mtproto/auth_key

and here

https://core.telegram.org/mtproto/samples-auth_key

Try to successfully generate an AuthKey.

This exercise will get you familiar with enough of the basics as well as help you build up routines you will need to do further work on Telegram API.

I have outlined the basics for you to get started in this SO post.

Also i think the API documentation online is not so well-written, but following the above step by step while reading the API documentation, for just AuthKey generation, would get you familiar with the language and writing style of the authors of the API

Good Luck.

Hubbs answered 27/9, 2015 at 15:10 Comment(2)
Is there any libraries for android.?Jolley
Like other APIs we have a particular code written by the API provider for just including that piece of code to your app and APIs are ready to be used. Is there any such thing for Telegram API? I just want to start from scratch.Snowstorm
B
21

The Telegram API is not as easy to use as a normal HTTP/Rest API, you have to interact with their MTProto protocol. You also have to do all sorts of encryption and decryption. Telegram recently released a new Bot API which abstracts all the complications behind a decent HTTP API. Usage example in NodeJS using https://github.com/arcturial/telegrambot:

var TelegramBot = require('telegrambot');
var api = new TelegramBot('<YOUR TOKEN HERE>');

api.getUpdates({ offset: 0 }, function (err, updates) {
    // array of message updates since last poll
    console.log(updates);
});

api.sendMessage({ chat_id: 0, text: 'test' }, function (err, message) {
    // the chat_id is the id received in the getUpdates() call
});

The token can be generated using their BotFather application. You can also use their deep-linking feature to add a link to your website to initiate a conversation with the bot, like so:

https://telegram.me/triviabot?start=payload

The payload value can be anything you want, like a cache key you might use for validating a real person etc.

I know it doesn't directly answer your question, but from personal experience I found it's better to interact with the Bot API than trying to implement all the intricacies required for the normal API. If you are adamant about using their normal API, the IPs are 149.154.167.40:443 (test) and 149.154.167.50:443 (production). They provide the IP details under https://my.telegram.org/apps.

Burney answered 5/7, 2015 at 10:5 Comment(4)
Thanks for the answer. My main goal is to write a simple Telegram Client which is able to send messages to contacts (by checking if the phone number is registered in Telegram). And I want to do this in C#. Which method do you recommend the most? Can the Bot API be used to invoke the methods in Telegram API (or methods of Bot API similar to those)?Oleson
The Bot API works well if you want to create an application that can integrate with Telegram (think Slack and Slack integrations), like seeings Jenkins/Travis CI build notifications display in your Telegram chat. Bots don't have to be assigned to a number. If you want to create a client that allows person to person message sending, then you have to use the normal Telegram API. Unfortunately I have had little luck getting it to work properly, I can only suggest you have a look at the samples github.com/zhukov/webogram or github.com/enricostara/telegram.linkBurney
@ChrisBrand I know this is an old post, but did you have any luck with the normal Telegram API? I am getting a "getaddrinfo ENOTFOUND api.telegram.org api.telegram.org:443" error when trying to POST to the Telegram API using nodejs. When I do it in Postman, it works fineCompurgation
Bot api is not complete( You can't call a number of API methods from bot apiPalliasse
G
12

I was looking for a quick solution to interact with Telegram API (not bot API which is limited) and integrate it with a python project. Found the following python client implementation which was a big help. Hope it helps someone. As others have mentioned, telegram API is complicated to understand, but you can get going with Telethon in a very short time without pre knowledge about the telegram API protocol.

https://github.com/LonamiWebs/Telethon

To install telethon just type:

pip install telethon

Here is a short code demonstrating how easy you can get to use the API to print recent chats:

enter image description here The example taken from telethon github page.

Gschu answered 4/6, 2017 at 13:55 Comment(0)
K
1

For .NET programmers, there is now WTelegramClient, that allows you to call Telegram Client APIs (connecting as a user, not bot).

The library is very complete but also very simple to use. Follow the README on GitHub for an easy introduction.

It requires a permanent connection, but it can be integrated in an ASP.net website. See this FAQ, it contains an example website.

Kaiserslautern answered 25/2, 2022 at 21:19 Comment(0)
F
-1

For any javascript users, use telegram package in npm

npm install telegram
Fernferna answered 13/9, 2022 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.