
To connect to Telegram using Delphi, you can use the Telegram Bot API to send and receive messages. Here's an example code snippet that shows how to send a message using the Telegram Bot API:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.StdCtrls, IdHTTP, IdSSL, IdSSLOpenSSL;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const
BOTTOKEN = 'yourbot_token';
CHATID = 'yourchat_id';
implementation
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
var
HTTP: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
Response: string;
RequestData: TStringList;
begin
HTTP := TIdHTTP.Create(nil);
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
HTTP.IOHandler := SSL;
HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
RequestData := TStringList.Create;
try
RequestData.Add('chatid=' + CHATID);
RequestData.Add('text=' + Edit1.Text);
Response := HTTP.Post('https://api.telegram.org/bot' + BOT_TOKEN + '/sendMessage', RequestData);
Memo1.Lines.Add(Response);
finally
RequestData.Free;
end;
finally
SSL.Free;
HTTP.Free;
end;
end;
end.
To use this code, you'll need to replace your_bot_token and your_chat_id with your own bot token and chat ID. You can obtain a bot token by creating a new bot in Telegram and following the instructions. You can obtain your chat ID by sending a message to your bot and then using the GetUpdates method of the Telegram Bot API to retrieve the chat ID from the response.
Note that this example uses the Indy HTTP library to make the API request and the OpenSSL library to establish a secure connection. You'll need to add these libraries to your Delphi project in order to use this code.
To find your bot token and chat ID, follow these steps:
Open Telegram and search for the BotFather bot. This is the official bot that can help you create and manage your own bots.
Start a chat with the BotFather and follow the instructions to create a new bot. You'll need to choose a name and a username for your bot.
Once your bot is created, the BotFather will give you a token. This token is what you'll use to authenticate your bot and send messages to Telegram. Make sure you keep this token secret, as anyone who has access to it can control your bot.
To find your chat ID, send a message to your bot from your Telegram account.
Use a tool such as Postman or cURL to make an API request to the following endpoint:
https://api.telegram.org/bot<yourbottoken>/getUpdatesThe response will contain information about your message, including the chat ID. Look for the
chatobject in the response, and find theidproperty. This is your chat ID.
Alternatively, you can use a Telegram bot library for Delphi such as the "DelphiTG" library to automate this process and easily send messages from your application.
