Telegram入门

  • 基本常识
    • 词汇
    • 聊天消息去向
    • 用户认证
  • updates
  • 得到聊天信息
  • CMake C++ projects for telegram Client
    • Td::TdStatic
      • Client Class
    • Td::TdJson,Td::TdJsonStatic
      • td_json_client.h
      • td_log.h
    • Td::TdCoreStatic
      • ClientActor Class
  • 参考

基本常识

词汇

telegram最主要的对象就是消息,目前为止有不止35种类型的消息内容,主要有:

  • 文本消息( messageText)
  • 图片消息(messagePhoto)
  • 截图(messageScreenshotTaken )

6种不同的成员状态:

  • chatMemberStatusAdministrator
  • chatMemberStatusBanned,
  • chatMemberStatusCreator,
  • chatMemberStatusLeft,
  • chatMemberStatusMember
  • chatMemberStatusRestricted.

四种不同的聊天形式:

  • private chats
  • basic groups
  • supergroups
  • secret chats

聊天消息去向

  • The correct order of chats in the chat list is maintained by TDLib, so the Application only needs to listen to updates that change the chat.order field and sort all chats by the pair (chat.order, chat.id) in a given chat.list.
  • TDLib通过updates向应用传递数据

用户认证

当用户需要认证时,TDLib向应用程序发送一个 updateAuthorizationState 为了描述目前的认证状态AuthorizationState
应用收到的第一种验证类型是authorizationStateWaitTdlibParameters,应用通过setTdlibParameters方法来给TDLib提供初始化参数。参数主要有:

  • api_id
  • api_hash
  • database_directory
  • use_message_database
  • use_secret_chats
  • system_language_code
  • device_model
  • system_version

如果验证成功,应用会收到新的updateAuthorizationState;如果失败,收到的updateAuthorizationState不会更新。
应用收到的第二种认证状态是authorizationStateWaitEncryptionKey。当收到此状态信息时,应用必须通过checkDatabaseEncryptionKey提供数据库加密秘钥。
完成这些认证之后,应用将会收到authorizationStateReady,意味着认证成功并可以进行request操作。

updates

updates列表和被处理的方法

  • updateAuthorizationState
  • updateNewChat:当有新的chat时,会收到此update。当应用收到chat_id时,就不需要用getChat请求去获得chat对象,会有一个缓存接收chat对象
  • updateUser:
  • updateBasicGroup
  • updateSupergroup
  • updateSecretChat
  • updateNewMessage
  • updateMessageSendSucceeded
  • updateMessageContent
  • updateFile

得到聊天信息

应用可以通过getChatHistory 方法得到一个chat中的信息。信息会以逆时间顺序返回(也就是message_id的降序)。应用传递from_message_id=0会得到最后一个消息。如果应用需要更多的消息,需要调整from_message_id参数并且不断请求。

CMake C++ projects for telegram Client

Td::TdStatic

一般方法的c++接口静态库

Client Class

通用的使用模式:

std::shared_ptr<td::Client> client = std::make_shared<td::Client>();
// somehow share the client with other threads, which will be able to send requests via client->send
const double WAIT_TIMEOUT = 10.0;  // seconds
bool is_closed = false;            // should be set to true, when updateAuthorizationState with// authorizationStateClosed is received
while (!is_closed) {auto response = client->receive(WAIT_TIMEOUT);if (response.object == nullptr) {continue;}if (response.id == 0) {// process response.object as an incoming update of type td_api::Update} else {// process response.object as an answer to a sent request with id response.id}
}
  1. 构造和析构函数:
  • Client() 创建TDLib客户端
  • ~Client() 销毁Client对象
  • Client(Client&& other) 拷贝构造函数
  1. 函数方法
  • void send(Request&& request)  从任何线程向TDLib发送请求
  • Response receive ( double timeout )  收到TDLib的updates或者response,但是该函数不能同时由两个不同的线程调用。timeout是允许等待的最大时间,超过则返回空指针。
  • static Response execute(Request&& request) 同步执行TDLib请求,返回请求的response

Td::TdJson,Td::TdJsonStatic

这是JSON接口的动态和静态版本

td_json_client.h

  • 通过td_json_client_create创建client实例。
  • 使用td_json_client_send 发送requests
  • td_json_client_receive 接收 updatesresponse,不能同时被两个线程调用
  • 使用td_json_client_execute执行TDLib请求可以实现线程间的同步
  • 使用td_json_client_destroy销毁client

通用使用模式:

void *client = td_json_client_create();
// somehow share the client with other threads, which will be able to send requests via td_json_client_send
const double WAIT_TIMEOUT = 10.0; // seconds
int is_closed = 0;  // should be set to 1, when updateAuthorizationState with authorizationStateClosed is received
while (!is_closed) {const char *result = td_json_client_receive(client, WAIT_TIMEOUT);if (result) {// parse the result as JSON object and process it as an incoming update or an answer to a previously sent request}
}
td_json_client_destroy(client);

函数接口:

void *   td_json_client_create ()void    td_json_client_send (void *client, const char *request)const char *     td_json_client_receive (void *client, double timeout)const char *   td_json_client_execute (void *client, const char *request)void  td_json_client_destroy (void *client)

td_log.h

管理TDLib内部日志记录的C接口

typedef void(* td_log_fatal_error_callback_ptr) (const char *error_message)

当致命错误发生时,调用该回调函数。error_message空终止字符串,描述刚发生的错误。

TDJSON_DEPRECATED_EXPORT int td_set_log_file_path    (   const char *    file_path   )

日志默认写到stderr或者一个操作系统具体日志,使用该方法可以重设日志路径。
成功返回1,不成功返回0

TDJSON_DEPRECATED_EXPORT void td_set_log_max_file_size   (   long long   max_file_size   )

设置文件的最大大小

TDJSON_DEPRECATED_EXPORT void td_set_log_verbosity_level (   int     new_verbosity_level )

设定日志的详细程度
等级0:致命错误。等级1:错误。等级2:警告和debug警告。等级3:信息性的。等级4:debug。等级5:详细debug。6-1024:更详细的记录。

void td_set_log_fatal_error_callback (   td_log_fatal_error_callback_ptr     callback    )

当发生致命错误时,callback 就会被调用。当移除调用时,将NULL传递给callback。

Td::TdCoreStatic

具有低级C ++接口的静态库,主要供内部使用

ClientActor Class

这个接口比Client类接口更加灵活,然而,对于大多数使用,Client已经够用了。

  1. 构造和析构函数
ClientActor(unique_ptr< TdCallback > callback)
\\callback:Callback for outgoing notifications from TDLib.
~ClientActor()ClientActor(ClientActor&& other)
\\拷贝构造
  1. 函数方法
void request(uint64 id, td_api::object_ptr< td_api::Function > request )
//向TDLib发送一个请求,答案将通过callback接收
//id:  request标识,必须为正数;   request:请求static td_api::object_ptr< td_api::Object > execute (td_api::object_ptr< td_api::Function > request)
//执行request请求,返回请求回应response

参考

https://core.telegram.org/tdlib/getting-started
https://core.telegram.org/tdlib/docs/classtd_1_1_client.html
https://github.com/tdlib/td#using-cxx
https://core.telegram.org/tdlib/docs/td__json__client_8h.html#a1fea1f986bf950d19eee3032c24cce83

Telegram入门相关推荐

  1. 两个月入门深度学习,全靠动手实践!一位前端小哥的经验分享

    两个月入门深度学习,全靠动手实践!一位前端小哥的经验分享   在当前社会,技术日新月异,一个全栈工程师不及时学习新知识,掌握AI技能,再过两年就算不上"全栈"了. 产品发烧友.前端 ...

  2. telegram 机器人_学习使用Python在Telegram中构建您的第一个机器人

    telegram 机器人 Imagine this, there is a message bot that will send you a random cute dog image wheneve ...

  3. telegram 机器人_我在周末构建了一个无服务器的Telegram机器人。 这是我学到的。...

    telegram 机器人 by Moses Soh 通过摩西·苏(Moses Soh) 我在周末构建了一个无服务器的Telegram机器人. 这是我学到的. (I built a serverless ...

  4. 渗透测试入门3之隐匿攻击

    渗透测试入门3之隐匿攻击 1. Command and Control ICMP :https://pentestlab.blog/2017/07/28/command-and-control-icm ...

  5. telegram 常见问题

    目录 一般的问题 问:什么是电报?我在这里做什么 问:谁是电报? 问:电报与WhatsApp不同? 问:电报多大? 问:我可以使用哪些设备? 问:电报背后的人是谁? 问:你有广告吗?还是出售我的数据? ...

  6. 赶紧收藏:如何使用Telegram客户支持

    想要使用Telegram需要客户支持?您需要了解的有关使用Telegram作为客户服务渠道的所有信息,本文章都会介绍.我们将首先讨论提供Telegram支持以及入门所需了解的内容.然后,我们将向您展示 ...

  7. 2018-8-10-dotnet-从入门到放弃的-500-篇文章合集

    title author date CreateTime categories dotnet 从入门到放弃的 500 篇文章合集 lindexi 2018-08-10 19:16:52 +0800 2 ...

  8. dotnet 从入门到放弃的 500 篇文章合集

    本文是记录我从入门到放弃写的博客 博客包括 C#.WPF.UWP.dotnet core .git 和 VisualStudio 和一些算法,所有博客使用 docx 保存 下载:dotnet 从入门到 ...

  9. 超零协议(SERO)轻松入门——基于CENTOS7

    1. 机器配置 以目前Beta上链的规模,满足以下配置 CPU 4线程以上 MEM 4GB以上 DISK 50G以上 就能很好的运行了. #本文中测试机配置 --------------------- ...

最新文章

  1. 阿里P8都留不住的程序员和他们的公众号!
  2. 手撕 CNN 经典网络之 VGGNet(理论篇)
  3. python数据分析第三方库是_python数据分析复盘——数据分析相关库之Pandas
  4. 深入浅出MFC:DDX_Control本质探究
  5. Apache 设置http跳转至HTTPS访问
  6. IIS不能发布asp.net 应用程序
  7. 算法导论课后习题解析 第四章 下
  8. Linux shell - 按时间和文件大小排序显示文件(ll)
  9. 支付宝开放生活频道 消费者可直达商家生活号、小程序
  10. HTML5的优点与缺点
  11. SQL极限函数limit()详解分页必备
  12. LLVM每日谈之二十一 一些关于编译器和LLVM/Clang的代码
  13. Sound Studio for Mac - 音频编辑处理工具
  14. 操作系统的不确定性是指_读文||不确定性原理—人工智能的哲学基础
  15. 大数据预测(大数据核心应用)
  16. 【华人学者风采】聂飞平 西北工业大学
  17. 行业专家对2021年的云计算发展趋势的预测
  18. 程序控制结构-飞机超速报警系统
  19. uniapp App跳转微信小程序并互相传递参数、接收微信小程序传递的参数
  20. 赚钱很难吗?死磕一个项目,10年,必定成神

热门文章

  1. 微信小程序:中老年用户群体的流量生意如何做?
  2. USB1.1 协议开发
  3. MATLAB注意事项
  4. 可以DIY装修的商城系统,你也能拥有
  5. android studio 导出 aar,Android Studio 导出 .aar包的操作流程
  6. 《艾尔登法环》雷亚卢卡利亚结晶坑道的位置
  7. 3D游戏编程 作业六 打飞碟改进
  8. 区块链将成为引导第四次工业革命的重要力量
  9. P9065 [yLOI2023] 云梦谣 题解
  10. 在OC项目下实现SwiftMonkey