![]() |
Telegram inline_keyboard example
Liste der Anhänge anzeigen (Anzahl: 1)
Telegram BotApi,
Nachdem ich mich dumm und dämlich nach einem Delphi example gesucht habe, stelle ich nun hier ein example rein, mit dem eine Message mit x Button an die API von Telegram senden kann.
Delphi-Quellcode:
procedure TTGMBot.btn2Click(Sender: TObject);
// uses // System.SysUtils, // System.Classes, // System.JSON.Types, // System.JSON.Writers, // System.JSON.Builders; var LIdHTTP: TIdHTTP; LsList: TStringList; LSr : TStringReader; LReader : TJsonTextReader; LBuilder: TJSONObjectBuilder; LWriter: TJsonTextWriter; LStringWriter: TStringWriter; LStringBuilder: TStringBuilder; const command = '/sendMessage'; text = 'text=test' ; chat_id = 'chat_id=-xxxxxxx';// Chat Id des destination Chat LBotApiKey = '64444444:AAxxxxxxxxxxxx_xxxxxxxxxxxxxx'; begin LStringBuilder := TStringBuilder.Create; try LStringWriter := TStringWriter.Create(LStringBuilder); try LWriter := TJsonTextWriter.Create(LStringWriter); try LBuilder := TJSONObjectBuilder.Create(LWriter); try LsList := TStringList.Create; try LsList.DefaultEncoding.UTF8; LsList.Add(chat_id) ; LsList.Add(text) ; LBuilder .BeginObject .BeginArray('inline_keyboard') .BeginArray() .BeginObject .Add('text', 'Yes') .Add('url', 'http://www.google.com/') .EndObject .BeginObject .Add('text', 'no') .Add('url', 'https://www.bing.com/') .EndObject .EndArray .EndArray .EndObject; LsList.Add('reply_markup=' + LStringBuilder.ToString) ; LIdHTTP:= TIdHTTP.Create(); try mm2(LIdHTTP.post('https://api.telegram.org/bot' + LBotApiKey + command ,LsList)); finally LIdHTTP.Free; end; LWriter.Formatting := TJsonFormatting.Indented; mm2(LStringBuilder.ToString); // mm2 = TMemo finally LsList.Free; end; finally LBuilder.Free; end; finally LWriter.Free; end; finally LStringWriter.Free; end; finally LStringBuilder.Free; end; end; Alternative ohne Builder
Delphi-Quellcode:
LStringWriter := TStringWriter.Create;
try LWriter := TJsonTextWriter.Create(LStringWriter); try LsList := TStringList.Create; try /// am ende soll es in Jsn_reply_markup_Inl LsList.DefaultEncoding.UTF8; LsList.Add(chat_id) ; LsList.Add(text) ; with LWriter do begin WriteStartObject; WritePropertyName('inline_keyboard'); WriteStartArray; WriteStartArray; WriteStartObject; WritePropertyName('text'); WriteValue('yes'); WritePropertyName('url'); WriteValue('http://www.google.com/'); WriteEndObject; WriteStartObject; WritePropertyName('text'); WriteValue('no'); WritePropertyName('url'); WriteValue('https://www.bing.com/'); WriteEndObject; WriteEndArray; WriteEndArray; WriteEndObject end; LsList.Add('reply_markup=' + LStringWriter.ToString) ; LIdHTTP:= TIdHTTP.Create(); try mm2(LIdHTTP.post('https://api.telegram.org/bot' + BotApiKey + command ,LsList)); finally LIdHTTP.Free; end; mm2('W=' + LStringWriter.ToString); finally LsList.Free; end; finally LWriter.Free; end; finally LStringWriter.Free; end; Response
Code:
und Ergebnis in Telegram
14:02:28 {"ok":true,"result":{"message_id":383,"from":{"id":xxxx,"is_bot":true,"first_name":"xxxxx","username":"xxxxx"},"chat":{"id":xxxxxx,"title":"BoTTestxxxx","type":"group","all_members_are_administrators":false},"date":1538740948,"text":"test"}}
14:02:28 {"inline_keyboard":[[{"text":"Yes","url":"http://www.google.com/"},{"text":"no","url":"https://www.bing.com/"}]]} ![]() Have Fun Gruß ScharfeMietze ;) |
AW: Telegram inline_keyboard example
Hier ist
![]() |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:54 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz