用indy delphi7发送表情符号

时间:2016-09-22 05:28:25

标签: delphi unicode delphi-7 emoji indy-9

我想在delphi 7上用indy 9.00.10发送表情符号。我使用tnt VCL控件。 我发现这个url http://apps.timwhitlock.info/emoji/tables/unicode用于unicode和bytes代码。 如何使用indy将此代码转换为delphi常量发送。

我使用这个delphi代码发送消息给电报机器人:

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;
emojies的

字节样本:

AERIAL_TRAMWAY = '\xf0\x9f\x9a\xa1';
AIRPLANE = '\xe2\x9c\x88';
ALARM_CLOCK = '\xe2\x8f\xb0';
ALIEN_MONSTER = '\xf0\x9f\x91\xbe';
抱歉英语不好!!!

1 个答案:

答案 0 :(得分:0)

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

  

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

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

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

Indy 9不支持Delphi 2009+或Unicode。 string的所有用法都假定为AnsiString,这是Delphi 7中的情况。任何AnsiString添加到TIdMultipartFormDataStreamTStrings,甚至是UTF -8编码的一个将由TIdHTTP原样传输。但是,没有选项可以向服务器指定字符串数据实际上使用UTF-8作为字符集。但是,根据文档:

  

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

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

如果您仍然遇到multipart/form-data问题,请考虑使用application/x-www-form-urlencoded(使用TIdHTTP.Post(TStrings))或application/json(使用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参数是WideString,它使用UTF-16,因此您应该能够发送任何Unicode文本,包括emojis。如果您尝试在代码中生成文本,请确保UTF-16正确编码任何非ASCII字符。例如,代码点U+1F601 GRINNING FACE WITH SMILING EYES是UTF-16中的宽字符$D83D $DE01

var
  Text: WideString;

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

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

  

支持所有数字HTML实体。

代码点U+1F601是HTML中的数字实体$#128513;

var
  Text: WideString;

Text := 'hi $#128513;'; // 'hi '
SendMessage('@channel', Text, 'HTML', False);