• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

被动回复用户消息

原文链接:http://www.cnblogs.com/devinlee/p/4282687.html

扫下方二维码关注,测试效果

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;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ubuntu14.04安装matlabr2013a发布时间:2022-07-18
下一篇:
Matlab问题汇总发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap