WeChat public platform - receive ordinary messages

WeChat public platform - receive ordinary messages

The basic interface for receiving messages in development mode can be used to receive text messages, picture messages, voice messages, video messages, small video messages, geographic location messages, and link messages sent by ordinary users.

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, 'If you have any questions, leave a message, we will reply you as soon as possible!');
end;

function ResponseImage(M: TMessage; X: IXMLDocument): RawByteString;
begin
Result := ReplyText(M, 'The picture you sent is beautiful!');
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, 'I didn't hear what you said, but your voice is very magnetic^_^');
end;
end;

function ResponseVideo(M: TMessage; X: IXMLDocument): RawByteString;
begin
Result := ReplyText(M, 'What video? Isn't it porn?');
end;

function ResponseLocation(M: TMessage; X: IXMLDocument): RawByteString;
begin
Result := ReplyText(M, 'Send me your location, not afraid that I will track you? Haha!');
end;

function ResponseLink(M: TMessage; X: IXMLDocument): RawByteString;
begin
Result := ReplyText(M, 'What link? No Trojan horse?');
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('Received video message...' + M.MsgType + ', ' + M.FromUserName);
end;
location:
begin
Result := ResponseLocation( M, X);
addlog('Received location message...' + M.MsgType + ', ' + M.FromUserName);
end;
link:
begin
Result := ResponseLink(M, X);
addlog('Received Link message...' + M.MsgType + ', ' + M.FromUserName);
end
else
begin
Result := '';
addlog('Received unknown message: ' + 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;
than;
than;
than;

Guess you like

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