Telegram TDLib 客户端
在 Delphi/C++ Builder 中构建 Telegram 用户账户或机器人应用程序。封装官方 Telegram TDLib,公开身份验证、聊天、消息和更新事件。
在 Delphi/C++ Builder 中构建 Telegram 用户账户或机器人应用程序。封装官方 Telegram TDLib,公开身份验证、聊天、消息和更新事件。
官方 Telegram TDLib(tdjson)的 Delphi/C++ Builder 封装器。驱动身份验证状态机,提供类型化的消息发送/接收事件,并通过 OnEvent 传递其他所有 TDLib 更新。
TsgcTDLib_Telegram
Windows, macOS, Linux, iOS, Android
Standard / Professional / Enterprise
设置 Telegram.API.ApiId / ApiHash 和 Telegram.PhoneNumber,绑定 OnAuthenticationCode 处理登录流程,并在 OnAuthorizationStatus 报告 authorizationStateReady 后发送消息。
uses
sgcLibs;
var
Telegram: TsgcTDLib_Telegram;
begin
Telegram := TsgcTDLib_Telegram.Create(nil);
Telegram.Telegram.API.ApiId := '123456';
Telegram.Telegram.API.ApiHash := 'your-api-hash';
Telegram.Telegram.PhoneNumber := '+34600111222';
Telegram.OnAuthenticationCode := TelegramAuthenticationCode;
Telegram.OnAuthorizationStatus := TelegramAuthorizationStatus;
Telegram.OnMessageText := TelegramMessageText;
Telegram.Active := True;
end;
procedure TForm1.TelegramAuthenticationCode(Sender: TObject;
var Code: string);
begin
Code := InputBox('Telegram', 'Enter the code you received', '');
end;
procedure TForm1.TelegramAuthorizationStatus(Sender: TObject;
const Status: string);
begin
if Status = 'authorizationStateReady' then
Telegram.GetChats;
end;
procedure TForm1.TelegramMessageText(Sender: TObject;
MessageText: TsgcTelegramMessageText);
begin
Memo1.Lines.Add(MessageText.ChatId + ': ' + MessageText.Text);
Telegram.SendTextMessage(MessageText.ChatId, 'received!');
end;
// include: sgcLibs.hpp
TsgcTDLib_Telegram *Telegram = new TsgcTDLib_Telegram(this);
Telegram->Telegram->API->ApiId = "123456";
Telegram->Telegram->API->ApiHash = "your-api-hash";
Telegram->Telegram->PhoneNumber = "+34600111222";
Telegram->OnAuthenticationCode = TelegramAuthenticationCode;
Telegram->OnAuthorizationStatus = TelegramAuthorizationStatus;
Telegram->OnMessageText = TelegramMessageText;
Telegram->Active = true;
void __fastcall TForm1::TelegramAuthenticationCode(TObject *Sender, AnsiString &Code)
{
Code = InputBox("Telegram", "Enter the code you received", "");
}
void __fastcall TForm1::TelegramAuthorizationStatus(TObject *Sender, const AnsiString Status)
{
if (Status == "authorizationStateReady")
Telegram->GetChats();
}
void __fastcall TForm1::TelegramMessageText(TObject *Sender, TsgcTelegramMessageText *MessageText)
{
Memo1->Lines->Add(MessageText->ChatId + ": " + MessageText->Text);
Telegram->SendTextMessage(MessageText->ChatId, "received!");
}
官方 TDLib(tdjson)共享库的 Delphi 外观——与官方 Telegram 客户端使用相同的协议。
组件驱动 TDLib 身份验证状态机:电话号码、验证码、可选的双因素验证密码,并将会话持久保存在本地数据库目录中。
SendTextMessage、SendPhotoMessage、SendVideoMessage、SendDocumentMessage 和 SendMessageAlbum 涵盖主要的 TDLib 消息内容类型。
GetChats、GetChat、SearchPublicChat 和 GetUser 以类型化响应的形式公开 Telegram 通讯录和已加入的频道/群组。
OnEvent 以原始事件名称加 JSON 的形式传递每个 TDLib updateXxx 事件——新消息、用户输入、频道帖子、编辑、删除——供下游路由使用,同时针对常见场景提供类型化的 OnMessageText、OnNewChat 和 OnNewCallbackQuery 事件。
使用官方 tdjson.dll / libtdjson.so / libtdjson.dylib——与官方 Telegram 客户端相同的端到端加密保障。
TDLib 支持 Windows、macOS、Linux、Android 和 iOS。组件会按平台自动定位并加载原生库;您只需通过上述事件和方法处理类型化的桥接。
本组件实现的 API 的权威来源。