zoukankan      html  css  js  c++  java
  • 微信公众平台——接收普通消息

    微信公众平台——接收普通消息

    开发模式下的接收消息基础接口,可用来接收普通用户发送的文本消息、图片消息、语音消息、视频消息、小视频消息、地理位置消息、链接消息。

    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;

  • 相关阅读:
    算法的时间与空间复杂度(一看就懂)
    技术人更应该学会即兴表达
    架构设计之「数据库集群方案」
    架构设计之「数据库从主备到主主的高可用方案」
    网络中的「动态路由算法」,你了解吗?
    当你「ping 一下」的时候,你知道它背后的逻辑吗?
    不懂高性能的负载均衡设计?没关系,架构师带你飞
    piwik流量统计系统搭建(apache2.4+piwik+mysql5.6+php5.6.14)
    SQL Server 之 GROUP BY、GROUPING SETS、ROLLUP、CUBE
    CAS 实现单点登录 .NET MVC
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/8987586.html
Copyright © 2011-2022 走看看