json 如何与 Telegram API 交互
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31228368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to interact with Telegram API
提问by Ramtin Soltani
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.
我真的很困惑,因为我在阅读了http://core.telegram.org上的大量文档后试图使用 Telegram 的 API 。
I have registered my app and got a hash_id and all of that stuff. But I'm not sure where to begin with.
我已经注册了我的应用程序并获得了 hash_id 和所有这些东西。但我不确定从哪里开始。
I had worked with Spotify's API before, and was able to interact with it using http://api.spotify.com/v1/method?params:valuesform.
我之前使用过 Spotify 的 API,并且能够使用http://api.spotify.com/v1/method?params:values表单与之交互。
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.
我找不到 Telegram 的 API 的 URL。我也在互联网上搜索了很多,但找不到任何有用的例子。
Does anyone know anything about getting started to work with Telegram's API? Any help would be appreciated.
有没有人知道如何开始使用 Telegram 的 API?任何帮助,将不胜感激。
采纳答案by Charles Okwuagwu
If you really want to understand Telegram API development from scratch. My advice would be to follow the steps here
如果你真的想从头开始了解 Telegram API 开发。我的建议是按照这里的步骤操作
https://core.telegram.org/mtproto/auth_key
https://core.telegram.org/mtproto/auth_key
and here
和这里
https://core.telegram.org/mtproto/samples-auth_key
https://core.telegram.org/mtproto/samples-auth_key
Try to successfully generate an AuthKey.
尝试成功生成 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.
本练习将使您熟悉足够的基础知识,并帮助您构建在 Telegram API 上做进一步工作所需的例程。
I have outlined the basics for you to get started in this SOpost.
我已经概述了您在这篇SO帖子中入门的基础知识。
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
另外我觉得网上的API文档写得不是那么好,但是在阅读API文档的同时按照上面的步骤一步一步来,对于AuthKey生成,会让你熟悉API作者的语言和写作风格
Good Luck.
祝你好运。
回答by Chris Brand
The Telegram API is not as easy to use as a normal HTTP/Rest API, you have to interact with their MTProtoprotocol. You also have to do all sorts of encryption and decryption. Telegram recently released a new Bot APIwhich abstracts all the complications behind a decent HTTP API. Usage example in NodeJSusing https://github.com/arcturial/telegrambot:
Telegram API 不像普通的 HTTP/Rest API 那样容易使用,你必须与他们的MTProto协议进行交互。您还必须进行各种加密和解密。Telegram 最近发布了一个新的Bot API,它抽象了一个体面的 HTTP API 背后的所有复杂性。在NodeJS 中使用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 BotFatherapplication. You can also use their deep-linking feature to add a link to your website to initiate a conversation with the bot, like so:
可以使用他们的BotFather应用程序生成令牌。您还可以使用他们的深层链接功能添加指向您网站的链接以启动与机器人的对话,如下所示:
https://telegram.me/triviabot?start=payload
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.
我知道它不能直接回答您的问题,但根据个人经验,我发现与 Bot API 交互比尝试实现普通 API 所需的所有复杂功能要好。如果你坚持使用他们的普通 API,IP 是149.154.167.40:443(测试)和149.154.167.50:443(生产)。他们在https://my.telegram.org/apps下提供 IP 详细信息。
回答by apadana
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.
我正在寻找一种快速的解决方案来与Telegram API(不是有限的 bot API)交互并将其与 python 项目集成。发现以下 python 客户端实现,这是一个很大的帮助。希望它可以帮助某人。正如其他人提到的,电报 API 很难理解,但是您可以在很短的时间内开始使用 Telethon,而无需事先了解电报 API 协议。
To install telethon just type:
要安装 Telethon,只需键入:
pip install telethon
Here is a short code demonstrating how easy you can get to use the API to print recent chats:
下面是一段简短的代码,演示了如何轻松地使用 API 打印最近的聊天记录:


