0

i want to send emoji with indy 9.00.10 on delphi 7. i use tnt VCL Controls . i found this url http://apps.timwhitlock.info/emoji/tables/unicode for unicode and bytes code. how to convert this codes to delphi Constants for Send with indy.

i use this delphi code for send message to telegram bot:

procedure TBotThread.SendMessage(ChatID:String; Text : WideString;
parse_mode:string;disable_notification:boolean);
Var
  Stream: TStringStream;
  Params: TIdMultipartFormDataStream;
  //Text : WideString;
  msg : WideString;
  Src : string;
  LHandler: TIdSSLIOHandlerSocket;
begin
  try
    try
      if FShowBotLink then
        Text := Text + LineBreak + FBotUser;
      msg := '/sendmessage';
      Stream := TStringStream.Create('');
      Params := TIdMultipartFormDataStream.Create;
      Params.AddFormField('chat_id',ChatID);
      if parse_mode <> '' then
        Params.AddFormField('parse_mode',parse_mode);
      if disable_notification then
        Params.AddFormField('disable_notification','true')
      else
        Params.AddFormField('disable_notification','false');
      Params.AddFormField('disable_web_page_preview','true');
      Params.AddFormField('text',UTF8Encode(Text));
      LHandler := TIdSSLIOHandlerSocket.Create(nil);
      FidHttpSend.ReadTimeout := 30000;
      FidHttpSend.IOHandler:=LHandler;
      LHandler.SSLOptions.Method := sslvTLSv1;
      LHandler.SSLOptions.Mode := sslmUnassigned;
      FidHttpSend.HandleRedirects := true;
      FidHttpSend.Post(BaseUrl + API + msg, Params, Stream);
    finally
      Params.Free;
      Stream.Free;
    ENd;
 except
   on E: EIdHTTPProtocolException do
   begin
      if E.ReplyErrorCode = 403 then
      begin
       WriteToLog('Bot was blocked by the user');
      end;
   end;
 end;  
end;

bytes sample for emojies:

AERIAL_TRAMWAY = '\xf0\x9f\x9a\xa1';
AIRPLANE = '\xe2\x9c\x88';
ALARM_CLOCK = '\xe2\x8f\xb0';
ALIEN_MONSTER = '\xf0\x9f\x91\xbe';

sorry for bad english!!!

4

1 回答 1

0

Telegram Bot API支持多种形式的输入:

我们支持GETHTTPPOST方法。我们支持四种方式在 Bot API 请求中传递参数:

  • 网址查询字符串
  • 应用程序/x-www-form-urlencoded
  • application/json(上传文件除外)
  • multipart/form-data(用于上传文件)

您正在使用最后一个选项。

Indy 9 不支持 Delphi 2009+ 或 Unicode。的所有用途string都假定为AnsiString,这在 Delphi 7 中就是这种情况。AnsiString您添加到TIdMultipartFormDataStream或的任何内容TStrings,甚至是 UTF-8 编码的,将由TIdHTTP. 但是,没有选项可以向服务器指定字符串数据实际上使用 UTF-8 作为字符集。但是,根据文档:

所有查询都必须使用 UTF-8 进行。

因此,不指定显式字符集可能不是问题。

如果您仍然有问题,请multipart/form-data考虑使用application/x-www-form-urlencoded(use TIdHTTP.Post(TStrings)) 或application/json(use TIdHTTP.Post(TStream)) 代替:

procedure TBotThread.SendMessage(ChatID: String; Text: WideString; parse_mode: string; disable_notification: boolean);
var
  Params: TStringList;
  LHandler: TIdSSLIOHandlerSocket;
begin
  if FShowBotLink then
    Text := Text + LineBreak + FBotUser;

  Params := TStringList.Create;
  try
    Params.Add('chat_id=' + UTF8Encode(ChatID));
    if parse_mode <> '' then
      Params.Add('parse_mode=' + UTF8Encode(parse_mode));
    if disable_notification then
      Params.Add('disable_notification=true')
    else
      Params.Add('disable_notification=false');
    Params.Add('disable_web_page_preview=true');
    Params.Add('text=' + UTF8Encode(Text));

    LHandler := TIdSSLIOHandlerSocket.Create(nil);
    try
      LHandler.SSLOptions.Method := sslvTLSv1;
      LHandler.SSLOptions.Mode := sslmClient;

      FidHttpSend.HandleRedirects := true;
      FidHttpSend.ReadTimeout := 30000;
      FidHttpSend.IOHandler := LHandler;
      try
        try
          FidHttpSend.Post(BaseUrl + API + '/sendmessage', Params, TStream(nil));
        except
          on E: EIdHTTPProtocolException do
          begin
            if E.ReplyErrorCode = 403 then
            begin
              WriteToLog('Bot was blocked by the user');
            end;
          end;
        end;  
      finally
        FidHttpSend.IOHandler := nil;
      end;
    finally
      LHandler.Free;
    end;
  finally
    Params.Free;
  end;
end;

procedure TBotThread.SendMessage(ChatID: String; Text: WideString; parse_mode: string; disable_notification: boolean);
var
  Params: TStringStream;
  LHandler: TIdSSLIOHandlerSocket;

  function JsonEncode(const wStr: WideString): string;
  var
    I: Integer;
    Ch: WideChar;
  begin
    // JSON uses UTF-16 text, so no need to encode to UTF-8...
    Result := '';
    for I := 1 to Length(wStr) do
    begin
      Ch := wStr[i];
      case Ch of
        #8: Result := Result + '\b';
        #9: Result := Result + '\t';
        #10: Result := Result + '\n';
        #12: Result := Result + '\f';
        #13: Result := Result + '\r';
        '"': Result := Result + '\"';
        '\': Result := Result + '\\';
        '/': Result := Result + '\/';
      else
        if (Ord(Ch) >= 32) and (Ord(Ch) <= 126) then
          Result := Result + AnsiChar(Ord(wStr[i]))
        else
          Result := Result + '\u' + IntToHex(Ord(wStr[i]), 4);
      end;
    end;
  end;

begin
  if FShowBotLink then
    Text := Text + LineBreak + FBotUser;

  Params := TStringStream.Create('');
  try
    Params.WriteString('{');
    Params.WriteString('chat_id: "' + JsonEncode(ChatID) + '",');
    if parse_mode <> '' then
      Params.WriteString('parse_mode: "' + JsonEncode(parse_mode) + '",')
    if disable_notification then
      Params.WriteString('disable_notification: True,')
    else
      Params.WriteString('disable_notification: False,');
    Params.WriteString('disable_web_page_preview: True,');
    Params.WriteString('text: "' + JsonEncode(Text) + '"');
    Params.WriteString('}');
    Params.Position := 0;

    LHandler := TIdSSLIOHandlerSocket.Create(nil);
    try
      LHandler.SSLOptions.Method := sslvTLSv1;
      LHandler.SSLOptions.Mode := sslmClient;

      FidHttpSend.HandleRedirects := true;
      FidHttpSend.ReadTimeout := 30000;
      FidHttpSend.IOHandler := LHandler;
      try
        try
          FidHttpSend.Request.ContentType := 'application/json';
          FidHttpSend.Post(BaseUrl + API + '/sendmessage', Params, TStream(nil));
        except
          on E: EIdHTTPProtocolException do
          begin
            if E.ReplyErrorCode = 403 then
            begin
              WriteToLog('Bot was blocked by the user');
            end;
          end;
        end;  
      finally
        FidHttpSend.IOHandler := nil;
      end;
    finally
      LHandler.Free;
    end;
  finally
    Params.Free;
  end;
end;

话虽如此,您的函数Text参数是 a WideString,它使用 UTF-16,因此您应该能够发送任何 Unicode 文本,包括表情符号。如果您尝试在代码中生成文本,只需确保 UTF-16 对任何非 ASCII 字符进行正确编码。例如,代码点是UTF-16U+1F601 GRINNING FACE WITH SMILING EYES中的宽字符:$D83D $DE01

var
  Text: WideString;

Text := 'hi ' + #$D83D#$DE01; // 'hi '
SendMessage('@channel', Text, 'Markup', False);

或者,您可以在文本消息中使用 HTML,这样您就可以使用数字 HTML 实体对非 ASCII 字符进行编码。根据文档:

支持所有数字 HTML 实体。

Codepoint是HTMLU+1F601中的数字实体:$#128513;

var
  Text: WideString;

Text := 'hi $#128513;'; // 'hi '
SendMessage('@channel', Text, 'HTML', False);
于 2016-09-22T20:14:23.520 回答