Delphi XE7 用indy开发微信公众平台(6)- 被动回复用户消息

被动回复用户消息

1、回复文本消息

function ReplyText(Msg: TMessage; MsgText: String): RawByteString;
var
  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;

//设置AResponseInfo.ContentText 等于返回的结果,即可自动回复文本消息

2、回复图文消息

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

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

function NewsMusic: TArray<TNews>;
begin
  SetLength(Result, 2);
  Result[0].Title := QQ音乐巅峰榜·内地;
  Result[0].Description := QQ音乐巅峰榜·内地;
  Result[0].PicUrl := MusicSubimg;
  Result[0].Url := http://y.qq.com/#type=toplist&p=top_2;

  Result[1].Title := QQ音乐巅峰榜·港台;
  Result[1].Description := QQ音乐巅峰榜·港台;
  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;
var
  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;

//设置AResponseInfo.ContentText 等于返回的结果,即可自动回复图文消息

3、回复模板消息

function PostMethod(HTTP: TIdHTTP; Url: String; Data: UTF8String; Max: Integer): String;
var
  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;

 

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

function ReplyTemRecharge(OpenID, AccessToken, First, Remark: String;
  Money: Integer): TJSONObject;
var
  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, 充值成功);
        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, 这只是模板测试,不要当真哦^_^);
        AddPair(color, #173177);
      end;
    end;
    temp := PostMethod(Url, UTF8Encode(Data.ToString), 1);
    Result := TJSONObject.ParseJSONValue(temp) as TJSONObject;
  finally
    Data.Free;
  end;
end;

 

原文地址:http://www.cnblogs.com/devinlee/p/4282687.html

Delphi XE7 用indy开发微信公众平台(6)- 被动回复用户消息

上一篇:Delphi XE7 用indy开发微信公众平台(9)- 高级群发接口


下一篇:Delphi XE7 用indy开发微信公众平台(4)- 接收事件推送