Einzelnen Beitrag anzeigen

ScharfeMietze

Registriert seit: 5. Mär 2014
165 Beiträge
 
Delphi 10.2 Tokyo Architect
 
#1

Telegram inline_keyboard example

  Alt 5. Okt 2018, 14:28
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:
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/"}]]}
und Ergebnis in Telegram
http://fs5.directupload.net/images/181005/8s75clm4.jpg

Have Fun
Gruß ScharfeMietze
Miniaturansicht angehängter Grafiken
unbenannt.jpg  

Geändert von ScharfeMietze ( 5. Okt 2018 um 16:41 Uhr)
  Mit Zitat antworten Zitat