WeChat public platform - passively reply to user messages

WeChat public platform - passively reply to user messages

The basic interface of reply information in development mode can be used to reply text messages, picture messages, voice messages, video messages, small video messages, geographical location messages, and link messages to users.

1. Reply to text messages

copy code
function ReplyText(Msg: TMessage; MsgText: String): RawByteString;
where
  X: IXMLDocument;
begin
  X := NewXMLDocument;
  try
    X.Xml.text := TextMsg;
    X.Active := true;
    with X.DocumentElement.ChildNodes do
    begin
      Nodes['ToUserName'].NodeValue := Msg.FromUserName;
      Nodes['FromUserName'].NodeValue := Msg.ToUserName;
      Nodes['CreateTime'].NodeValue := UnixTime(now);
      Nodes['MsgType'].NodeValue := 'text';
      Nodes['Content'].NodeValue := MsgText;
    end;
    Result := UTF8Encode(X.Xml.text);
  finally
    X.Active := False;
    X := nil;
  end;
end; 

//Set AResponseInfo.ContentText equal to the returned result to automatically reply to text messages
copy code

2. Reply to text messages

copy code
  TNews = record
    Title: String;
    Description: String;
    PicUrl: String;
    Url: String;
  end;

where
    MusicSubimg='https://mmbiz.qlogo.cn/mmbiz/cHdclURXy6gmu3ib6UA20b5s2Xy1Ra7cxYMNIuDvFDiaQvX8MZDibgQ1eCOsfvL6zufo7xSzUiaQZUY1KovYWmJ6Hg/0';

NewsMusic function: TArray <TNews>;
begin
  SetLength(Result, 2);
  Result[0].Title := 'QQ Music Top List·Mainland';
  Result[0].Description := 'QQ Music Top List·Mainland';
  Result[0].PicUrl := MusicSubimg;
  Result[0].Url := 'http://y.qq.com/#type=toplist&p=top_2';

  Result[1].Title := 'QQ Music Top Chart Hong Kong and Taiwan';
  Result[1].Description := 'QQ Music Top Chart Hong Kong and Taiwan';
  Result[1].PicUrl := MusicSubimg;
  Result[1].Url := 'http://y.qq.com/#type=toplist&p=top_1';
end;

function ReplyNews(M: TMessage; News: TArray<TNews>): RawByteString;
where
  X: IXMLDocument;
  I: Integer;
begin
  X := NewXMLDocument;
  try
    X.Xml.text := NewsMsg;
    X.Active := true;
    with X.DocumentElement.ChildNodes do
    begin
      Nodes['ToUserName'].NodeValue := M.FromUserName;
      Nodes['FromUserName'].NodeValue := M.ToUserName;
      Nodes['CreateTime'].NodeValue := UnixTime(now);
      Nodes['MsgType'].NodeValue := 'news';
      Nodes['ArticleCount'].NodeValue := length(News);
      with Nodes['Articles'].ChildNodes do
      begin
        with Nodes['item'].ChildNodes do
        begin
          Nodes['Title'].NodeValue := News[0].Title;
          Nodes['Description'].NodeValue := News[0].Description;
          Nodes['PicUrl'].NodeValue := News[0].PicUrl;
          Nodes['Url'].NodeValue := News[0].Url;
        end;
        for I := 1 to length(News) - 1 do
        begin
          Add(First.CloneNode(true));
          with Nodes['item'].ChildNodes do
          begin
            Nodes['Title'].NodeValue := News[I].Title;
            Nodes['Description'].NodeValue := News[I].Description;
            Nodes['PicUrl'].NodeValue := News[I].PicUrl;
            Nodes['Url'].NodeValue := News[I].Url;
          end;
        end;
      end;
    end;
    Result := UTF8Encode(X.Xml.text);
  finally
    X.Active := False;
    X := nil;
  end;
end; 

//Set AResponseInfo.ContentText equal to the returned result, you can automatically reply to the text message
copy code

3. Reply to template messages

copy code
function PostMethod(HTTP: TIdHTTP; Url: String; Data: UTF8String; Max: Integer): String;
where
  PostData, RespData: TStringStream;
begin
  RespData := TStringStream.Create('');
  PostData := TStringStream.Create(Data);
  try
    try
      if HTTP = nil then
        Exit;
      HTTP.Post(Url, PostData, RespData);
      Result := RespData.DataString;
      HTTP.Request.Referer := Url;
    except
      Dec(Max);
      if Max = 0 then
      begin
        Result := '';
        Exit;
      end;
      Result := PostMethod(Url, Data, Max);
    end;
  finally
    HTTP.Disconnect;
    FreeAndNil(RespData);
    FreeAndNil (PostData);
  end;
end;
copy code

 

copy code
SendTemplateUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s';

function ReplyTemRecharge(OpenID, AccessToken, First, Remark: String;
  Money: Integer): TJSONObject;
where
  Url: string;
  Data: TJSONObject;
  temp: RawByteString;
begin
  Data := TJSONObject.Create;
  try
    Url := Format(SendTemplateUrl, [AccessToken]);
    Data.AddPair('touser', OpenID);
    Data.AddPair('template_id', 'NH_ctxX4kjW1Jw3q8Cb1y1uBAard9uOMdF5F2Nq9Uwc');
    Data.AddPair('url', '');
    Data.AddPair('topcolor', '#FF0000');
    Data.AddPair('data', TJSONObject.Create);
    with Data.Values['data'] as TJSONObject do
    begin
      AddPair('first', TJSONObject.Create);
      with Values['first'] as TJSONObject do
      begin
        AddPair('value', 'recharge successful');
        AddPair('color', '#173177');
      end;
      AddPair('keyword1', TJSONObject.Create);
      with Values['keyword1'] as TJSONObject do
      begin
        AddPair('value', format('%d 元',[Money]));
        AddPair('color', '#173177');
      end;
      AddPair('keyword2', TJSONObject.Create);
      with Values['keyword2'] as TJSONObject do
      begin
        AddPair('value', formatdatetime('yyyy-mm-dd hh:mm:ss', now));
        AddPair('color', '#173177');
      end;
      AddPair('remark', TJSONObject.Create);
      with Values['remark'] as TJSONObject do
      begin
        AddPair('value', 'This is just a template test, don't take it seriously ^_^');
        AddPair('color', '#173177');
      end;
    end;
    temp := PostMethod(Url, UTF8Encode(Data.ToString), 1);
    Result := TJSONObject.ParseJSONValue(temp) as TJSONObject;
  finally
    Data.Free;
  end;
end;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325289985&siteId=291194637
Recommended