python微信平台自动回复信息如何写,python自动回复,就是比方有用户加了关注之


就是比方有用户加了关注之后 并没有发送任何消息 然后由后台主动发送一条欢迎信息 微信中有内嵌被关注这个事件吗 还是有其他的方法 我找到一个类似的文章 但是是php的 也没怎么看懂 麻烦有没有用python的高人可以解释一下怎么做啊

传送门http://www.cnblogs.com/txw1958/archive/2013/04/01/weixin-if26-subscribe.html

被用户关注之后,微信会发送一个 MsgType 为 Event 、Event 为 subscribe 的事件(你给的文章里已经写到了啊)

用 Bottle 写出来的话,大概是这样的:

from bottle import Bottle, request, responsefrom xml.etree import ElementTreeapp = Bottle()@app.post('/')def handle():    xml = request.body.read()    msg = dict((child.tag, to_unicode(child.text))              for child in ElementTree.fromstring(xml))    if msg.get("MsgType", None) == "Event" and msg.get("Event", None) == "subscribe":        #被用户关注        pass

为什么不用框架开发呢?比如 WeRoBot

推荐werobot

捕获MsgType,如果为event,表明这是一个subscribe的事件,就可以这样处理:

# 纯文本格式textTpl = """<xml>         <ToUserName><![CDATA[%s]]></ToUserName>         <FromUserName><![CDATA[%s]]></FromUserName>         <CreateTime>%s</CreateTime>         <MsgType><![CDATA[text]]></MsgType>         <Content><![CDATA[%s]]></Content>         <FuncFlag>0</FuncFlag>         </xml>"""# 判断MsgType内容,如果等于"event",表明是一个新关注用户if msg["MsgType"] == "event":    echostr = textTpl % (        msg['FromUserName'], msg['ToUserName'], str(int(time.time())),        u"这里替换为你想返回给用户的信息")    return echostr

微信关注后会发送一个关注的事件,你只要捕获这个事件给预回复就好了?不会python,下面这个是我用着的,php写的。

public function receiveEvent($object){    $recEve = $object->Event;    if($recEve == 'subscribe'){        $contentStr='感谢关注XXXXXX。';    }    $resultStr = $this->transmitText($object,$contentStr);    return $resultStr;}

回完帖子才点了一下你那个链接,发现方法是一样的。请问你哪里不懂呢?

编橙之家文章,

评论关闭