Delphi XE7 用indy开发微信公众平台(4)- 接收普通消息

接收普通消息

type
  TMsgType = (event, text, image, voice, video, location, link);

  TMessage = Record
    ToUserName: String;
    FromUserName: String;
    CreateTime: Integer;
    MsgType: String;
  end;

uses System.SysUtils, System.JSON, TypInfo, Xml.XMLIntf, Xml.XMLDoc, ActiveX;

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;

function ResponseText(M: TMessage; X: IXMLDocument): RawByteString;
begin
      Result := ReplyText(M, ‘有什么问题留言吧,我们会尽快答复您!‘);
end;

function ResponseImage(M: TMessage; X: IXMLDocument): RawByteString;
begin
  Result := ReplyText(M, ‘您发的图片很漂亮!‘);
end;

function ResponseVoice(M: TMessage; X: IXMLDocument): RawByteString;
begin
  try
    with X.DocumentElement.ChildNodes do
    begin
      Result := ReplyText(M, Format(VoiceMsg,
        [Nodes[‘Recognition‘].NodeValue]));
    end;
  except
    Result := ReplyText(M, ‘没听清您说什么,不过您的声音很有磁性^_^‘);
  end;
end;

function ResponseVideo(M: TMessage; X: IXMLDocument): RawByteString;
begin
  Result := ReplyText(M, ‘什么视频?不会是A片吧?‘);
end;

function ResponseLocation(M: TMessage; X: IXMLDocument): RawByteString;
begin
  Result := ReplyText(M, ‘把你的位置发给我了,不怕我跟踪你?哈哈!‘);
end;

function ResponseLink(M: TMessage; X: IXMLDocument): RawByteString;
begin
  Result := ReplyText(M, ‘什么链接?不会木马吧?‘);
end;

procedure AddLog(S: String);
begin
  Form1.Log.Lines.Add(formatdatetime(TimeFormat, now) + ‘: ‘ + S);
end;

function Response(M: TMessage; X: IXMLDocument): RawByteString;
var
  MsgType: TMsgType;
begin
  MsgType := TMsgType(GetEnumValue(TypeInfo(TMsgType), M.MsgType));
  case MsgType of
    event:
      begin
        Result := ResponseEvent(M, X);
      end;
    text:
      begin
        Result := ResponseText(M, X);
        addlog(‘收到文本消息...‘ + M.MsgType + ‘, ‘ + M.FromUserName);
      end;
    image:
      begin
        Result := ResponseImage(M, X);
        addlog(‘收到图片消息...‘ + M.MsgType + ‘, ‘ + M.FromUserName);
      end;
    voice:
      begin
        Result := ResponseVoice(M, X);
        addlog(‘收到语音消息...‘ + M.MsgType + ‘, ‘ + M.FromUserName);
      end;
    video:
      begin
        Result := ResponseVideo(M, X);
        addlog(‘收到视频消息...‘ + M.MsgType + ‘, ‘ + M.FromUserName);
      end;
    location:
      begin
        Result := ResponseLocation(M, X);
        addlog(‘收到位置消息...‘ + M.MsgType + ‘, ‘ + M.FromUserName);
      end;
    link:
      begin
        Result := ResponseLink(M, X);
        addlog(‘收到链接消息...‘ + M.MsgType + ‘, ‘ + M.FromUserName);
      end
  else
    begin
      Result := ‘‘;
      addlog(‘收到未知消息:‘ + M.MsgType + ‘, ‘ + M.FromUserName);
    end;
  end;
end;

function Analysis(Stream: TStream): RawByteString;
var
  X: IXMLDocument;
  M: TMessage;
begin
  try
    X := NewXMLDocument;
    X.Xml.BeginUpdate;
    X.Xml.text := StreamToString(Stream);
    X.Xml.EndUpdate;
    X.Active := true;
    with X.DocumentElement.ChildNodes do
    begin
      M.ToUserName := Nodes[‘ToUserName‘].NodeValue;
      M.FromUserName := Nodes[‘FromUserName‘].NodeValue;
      M.CreateTime := Nodes[‘CreateTime‘].NodeValue;
      M.MsgType := Nodes[‘MsgType‘].NodeValue;
    end;
    Result := Response(M, X);
  finally
    X.Active := False;
    X := nil;
  end;
end;

procedure Form1.IdHTTPServerCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  if CheckSignature(ARequestInfo) then
    if ARequestInfo.Params.Values[‘echostr‘] <> ‘‘ then
    begin
      AResponseInfo.ContentType := ‘text/html; charset=UTF-8‘;
      AResponseInfo.ContentText := ARequestInfo.Params.Values[‘echostr‘];
    end
    else
    begin
      if ARequestInfo.PostStream <> nil then
      begin
        CoInitialize(nil);
        try
          AResponseInfo.ContentType := ‘text/html; charset=UTF-8‘;
          AResponseInfo.ContentText := Analysis(ARequestInfo.PostStream);
        finally
          CoUninitialize;
        end;
      end;
    end;
end;

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

时间: 2024-10-26 12:24:59

Delphi XE7 用indy开发微信公众平台(4)- 接收普通消息的相关文章

Delphi XE7 用indy开发微信公众平台所有功能,可刷阅读,可刷赞,可加推广(除微支付)

http://www.cnblogs.com/devinlee/p/4565933.html Delphi XE7 用indy开发微信公众平台所有功能,可刷阅读,可刷赞,可加推广(除微支付) 关注作者的微信公众平台,测试效果 包含微信公众平台的所有功能(除微支付) 示例代码:(如需要全部代码,请加作者微信:Leedege) 复制代码 function UpNews(Num: Integer; AccessToken: String): String; var J: TJSONObject; N:

Delphi XE7 用indy开发微信公众平台(2)- 获取微信服务器IP地址

获取微信服务器IP地址 const ServerIpUrl = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?&access_token=%s'; procedure GetServerList(out List: TStringList; AccessToken: String); var Url: string; J: TJSONObject; temp: String; begin Url := Format(ServerIpUrl, [

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

接收事件推送 type TEventType = (subscribe, unsubscribe, SCAN, LOCATION, CLICK, VIEW); function ResponseEvent(M: TMessage; X: IXMLDocument): RawByteString; var event: TEventType; temp, menu: String; begin temp := X.DocumentElement.ChildNodes.Nodes['Event'].

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

Delphi XE7 用indy开发微信公众平台(8)- 自定义菜单

自定义菜单创建接口 1.创建菜单 const CreateMenuUrl = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s'; function CreateMenus(AccessToken: String): TJSONObject; var Url: string; J: TJSONObject; m1: TJSONObject; m2, m21, m22: TJSONObject; m3, m31, m32,

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

高级群发接口 1.上传多媒体文件(这里以上传图片为例) uses IdMultipartFormData; const UpMediaUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s'; function UpMedia(AccessToken, MediaType, MediaFile: String): String; var J: TJSONObject; Url: Strin

Delphi XE7 用indy开发微信公众平台(7)- 用户管理

用户管理 1.获取用户列表 const FansListUrl = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s'; function GetOpenIDList(AccessToken, NextOpenID: String): TStringList; var Url: string; J: TJSONObject; O: TJSONArray; temp: String; begin R

Delphi XE7 用indy开发微信公众平台(3)- 验证消息真实性

验证消息真实性 uses IdHashSHA, IdGlobal; function SHA1(Input: String): String; begin with TIdHashSHA1.Create do try Result := LowerCase(HashBytesAsHex(TidBytes(Bytesof(Input)))); finally Free; end; end; function CheckSignature(ARequestInfo: TIdHTTPRequestIn

Delphi XE7 用indy开发微信公众平台(1)- 获取access_token、expires_in

获取access_token.expires_in function GetMethod(HTTP: TIdHTTP; Url: String; Max: Integer): String; var RespData: TStringStream; begin RespData := TStringStream.Create('', TEncoding.UTF8); try try HTTP.Get(Url, RespData); HTTP.Request.Referer := Url; Res