Home > Back-end >  Ever which development receive send WeChat messages content module?
Ever which development receive send WeChat messages content module?

Time:09-29

Have any master using DELPHI development in their own program to send and receive WeChat content?

If yes, please grant instruction!

Be very grateful!

CodePudding user response:

Using TIdHTTPWebBrokerBridge
Response WeChat server GET and POST over data
But the advice is to use PHP or ASP.NET to do
Under a code before, do not remember the original author, thanks to the source author
 
Procedure TWebModule1. WebModule1CheckSignatureAction (Sender: TObject;
Request: TWebRequest; Response: TWebResponse; Var Handled: Boolean);
Var
Signature: string;
Timestamp: string;
Nonce: string;
Echostr: string;
STRS: TStringList;
TmpStr: string;
The begin
Signature:=Request. QueryFields. Values [' signature '];
Timestamp:=Request. QueryFields. Values [' timestamp];
Nonce:=Request. QueryFields. Values [' nonce];
Echostr:=Request. QueryFields. Values [' echostr];

The STRS: TStringList=. Create;

STRS. Add (Token);
STRS. Add (timestamp);
STRS. Add (nonce);

STRS. Sort;
TmpStr:=STRS STRS [0] + [1] + STRS [2].
TmpStr:=SHA1 (tmpStr);

If tmpStr=signature then
The Response. The Content:=echostr
The else
The Response. The Content:=' ';

STRS. Destroy;
end;

//the default HTTP service response to test for a normal HTTP service
Procedure TWebModule1. WebModule1DefaultHandlerAction (Sender: TObject;
Request: TWebRequest; Response: TWebResponse; Var Handled: Boolean);
The begin

The Response. The Content:='& lt; html> <meta HTTP - equiv="content-type" Content="text/HTML. Charset="GB2312"/& gt; <body> The server normal oh & lt;/body> ';
end;

//WeChat response
Procedure TWebModule1. WebModule1wechatAction (Sender: TObject;
Request: TWebRequest; Response: TWebResponse; Var Handled: Boolean);
Var
XmlRequest: IXMLDocument;
XmlResponse: IXMLDocument;
Node: IXMLNode;
TmpContent: RawByteString;
FromUserName: string;
ToUserName: string;
Content: the String;
MsgType: string;
CoResult: Integer;
The begin
TmpContent:=Request. The Content;
AddRequestContent (tmpContent);

//in multi-threaded applications using MSXML based on ActiveX,
to execute the following statementsCoInitializeEx (Nil, COINIT_MULTITHREADED);
If not ((CoResult=S_OK) or (CoResult=S_FALSE)) then
The begin
AddLog (' Failed to initialize the COM library. ');
The Response. The Content:=' ';
The Exit;
end;

//interpretation from WeChat push messages, this example only response text message
Try
XmlRequest:=NewXMLDocument;
AddLog (' Create Request XMLDocument Object Ok. ');

XmlRequest. XML. BeginUpdate;
XmlRequest. XML. Text:=tmpContent;
XmlRequest. XML. EndUpdate;
AddLog (' Set xmlRequest. The Text from the Request, the Content of Ok. ');

XmlRequest. Active:=True;
AddLog (' Set xmlRequest Active Ok. ');

ToUserName:=xmlRequest DocumentElement..childnodes. Nodes [' toUserName] NodeValue;
AddLog (' ToUserName: '+ ToUserName);

FromUserName:=xmlRequest DocumentElement..childnodes. Nodes [' fromUserName] NodeValue;
AddLog (+ FromUserName FromUserName:);

MsgType:=xmlRequest DocumentElement..childnodes. Nodes [' msgType] NodeValue;
AddLog (" MsgType: + MsgType);

If msgType='text' then
The begin
Content:=xmlRequest DocumentElement..childnodes. Nodes [' content '] NodeValue;
AddLog (' Content: '+ Content);
end;
Except,
On E: the Exception do Addlog (E.M essage);
end;

XmlRequest. Active:=False;
XmlRequest:=nil;

//reply to the sender of the message structure, in this case only for reply text message
Try
XmlResponse:=NewXMLDocument;
AddLog (' Create Response XMLDocument Object Ok. ');

XmlResponse. XML. Text:='& lt; Xml> 12345678 & lt;/CreateTime> 0 ';
AddLog (' Set xmlResponse. The Text to the Default Test - XML Message Ok. ');

XmlResponse. Active:=true;
AddLog (' Set xmlResponse Active Ok. ');

Node:=xmlResponse DocumentElement..childnodes. Nodes [' ToUserName];
Node. NodeValue:=fromUserName;
AddLog (' Set ToUserName to: + fromUserName);

Node:=xmlResponse DocumentElement..childnodes. Nodes [' FromUserName];
Node. NodeValue:=toUserName;
AddLog (' Set FormUserName to: + toUserName);

Node:=xmlResponse DocumentElement..childnodes. Nodes (" CreateTime ");
Node. NodeValue:=UnixTime (Now);
AddLog (' Set CreateTime to: + Node. The NodeValue);

Node:=xmlResponse DocumentElement..childnodes. Nodes [' MsgType];
Node. NodeValue:='text';
AddLog (' Set MsgType to: + Node. The NodeValue);

Node:=xmlResponse DocumentElement..childnodes. Nodes [' Content '];
Node. NodeValue:='hello! '# 10' welcome ';
AddLog (' Set the Content to: + Node. The NodeValue);

Node:=xmlResponse DocumentElement..childnodes. Nodes [' FuncFlag];
Node. NodeValue:=0;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related