繁体   English   中英

使用Indy delphi7发送表情符号

[英]Send emoji with indy delphi7

我想在delphi 7上发送印有9.00.10的表情符号。我使用tnt VCL控件。 我发现此网址http://apps.timwhitlock.info/emoji/tables/unicode中的unicode和字节码。 如何将此代码转换为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;

表情符号的字节样本:

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

对不起,英语不好!!!

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

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

  • URL查询字符串
  • 应用程序/ x-www-form-urlencoded
  • application / json(上传文件除外)
  • 多部分/表单数据(用于上传文件)

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

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文本,包括表情符号。 如果要在代码中生成文本,只需确保UTF-16正确编码了任何非ASCII字符。 例如,代码点U+1F601 GRINNING FACE WITH SMILING EYES $D83D $DE01是UTF-16中的宽字符$D83D $DE01

var
  Text: WideString;

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

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

支持所有数字HTML实体。

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

var
  Text: WideString;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM