微信公众平台——被动回复用户消息
开发模式下的回复信息基础接口,可用来向用户回复文本消息、图片消息、语音消息、视频消息、小视频消息、地理位置消息、链接消息。
1、回复文本消息
function ReplyText(Msg: TMessage; MsgText: String): RawByteString; varX: IXMLDocument; beginX := NewXMLDocument;tryX.Xml.text := TextMsg;X.Active := true;with X.DocumentElement.ChildNodes dobeginNodes['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);finallyX.Active := False;X := nil;end; end;
//设置AResponseInfo.ContentText 等于返回的结果,即可自动回复文本消息
2、回复图文消息
TNews = recordTitle: String;Description: String;PicUrl: String;Url: String;end;var MusicSubimg='https://mmbiz.qlogo.cn/mmbiz/cHdclURXy6gmu3ib6UA20b5s2Xy1Ra7cxYMNIuDvFDiaQvX8MZDibgQ1eCOsfvL6zufo7xSzUiaQZUY1KovYWmJ6Hg/0';function NewsMusic: TArray<TNews>; beginSetLength(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; varX: IXMLDocument;I: Integer; beginX := NewXMLDocument;tryX.Xml.text := NewsMsg;X.Active := true;with X.DocumentElement.ChildNodes dobeginNodes['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 dobeginwith Nodes['item'].ChildNodes dobeginNodes['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 dobeginAdd(First.CloneNode(true));with Nodes['item'].ChildNodes dobeginNodes['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);finallyX.Active := False;X := nil;end; end;
//设置AResponseInfo.ContentText 等于返回的结果,即可自动回复图文消息
3、回复模板消息
function PostMethod(HTTP: TIdHTTP; Url: String; Data: UTF8String; Max: Integer): String; varPostData, RespData: TStringStream; beginRespData := TStringStream.Create('');PostData := TStringStream.Create(Data);trytryif HTTP = nil thenExit;HTTP.Post(Url, PostData, RespData);Result := RespData.DataString;HTTP.Request.Referer := Url;exceptDec(Max);if Max = 0 thenbeginResult := '';Exit;end;Result := PostMethod(Url, Data, Max);end;finallyHTTP.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; varUrl: string;Data: TJSONObject;temp: RawByteString; beginData := TJSONObject.Create;tryUrl := 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 dobeginAddPair('first', TJSONObject.Create);with Values['first'] as TJSONObject dobeginAddPair('value', '充值成功');AddPair('color', '#173177');end;AddPair('keyword1', TJSONObject.Create);with Values['keyword1'] as TJSONObject dobeginAddPair('value', format('%d 元',[Money]));AddPair('color', '#173177');end;AddPair('keyword2', TJSONObject.Create);with Values['keyword2'] as TJSONObject dobeginAddPair('value', formatdatetime('yyyy-mm-dd hh:mm:ss', now));AddPair('color', '#173177');end;AddPair('remark', TJSONObject.Create);with Values['remark'] as TJSONObject dobeginAddPair('value', '这只是模板测试,不要当真哦^_^');AddPair('color', '#173177');end;end;temp := PostMethod(Url, UTF8Encode(Data.ToString), 1);Result := TJSONObject.ParseJSONValue(temp) as TJSONObject;finallyData.Free;end; end;