折腾:
期间,需要把已经得到的,二进制的二维码,
作为图片发送给用户
此处是Flask的后台服务器,用到了wechat-python-sdk
这个微信的SDK
其中又可能会涉及:
如何保存该图片?
那么可以参考:
去代码中看看:
直接就可以看到有:
send_image_message
def send_image_message(self, user_id, media_id): """ 发送图片消息 详情请参考 http://mp.weixin.qq.com/wiki/7/12a5a320ae96fecdf0e15cb06123de9f.html :param user_id: 用户 ID, 就是你收到的 WechatMessage 的 source :param media_id: 图片的媒体ID。 可以通过 :func:`upload_media` 上传。 :return: 返回的 JSON 数据包 """ return self.request.post( url=’https://api.weixin.qq.com/cgi-bin/message/custom/send’, data={ ‘touser’: user_id, ‘msgtype’: ‘image’, ‘image’: { ‘media_id’: media_id, }, } ) |
对应着官网的:
回复图片消息
很明显,需要:
“通过素材管理接口上传多媒体文件,得到的id。”
然后才能发送图片给用户。
所以去:
搜:
media/upload
看到:
def upload_media(self, media_type, media_file, extension=”): """ 上传多媒体文件 详情请参考 http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html :param media_type: 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb) :param media_file: 要上传的文件,一个 File object 或 StringIO object :param extension: 如果 media_file 传入的为 StringIO object,那么必须传入 extension 显示指明该媒体文件扩展名,如 “mp3“, “amr“;如果 media_file 传入的为 File object,那么该参数请留空 :return: 返回的 JSON 数据包 """ if six.PY2: return self._upload_media_py2(media_type, media_file, extension) else: return self._upload_media_py3(media_type, media_file, extension) |
注意:
调用此接口必须是file或StringIO
而此处的是str:
type(respQrcodeContent)=<type ‘str’>, respQrcodeContent= |
-》看来还是去保存二维码图片数据到本地临时文件,然后再去打开当作文件,然后再上传
[已解决]使用wechat-python上传图片的media出错:OfficialAPIError 41005 media data missing hint ScXL0536e292
然后就可以上传图片临时资源,得到对应的media_id了:
uploadMediaResp={u’media_id’: u’6-_Mx74k2P7B6cmOcbSlyZHkqTQbLj9y-CXUlPY3ltxGckEvbj3YT8fKx2dkNE2X’, u’created_at’: 1472215847, u’type’: u’image’} |
然后,通过代码:
/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/templates/showEvent.html
<form action="{{ url_for("create_event_qrcode", eventId=curEvent.id, curUserOpenid=curEvent.user.openid) }}" method="POST"> <input type="submit" value="生成邀请码" class="button button-big button-fill color-blue"/> </form> |
/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/views.py
@app.route(‘/create_event_qrcode’, methods=[‘GET’, ‘POST’]) def create_event_qrcode(): requestMethod = request.method requestArgs = request.args app.logger.debug(‘requestMethod=%s, requestArgs=%s’, requestMethod, requestArgs) curUserOpenid = request.args.get("curUserOpenid", "") app.logger.debug(‘curUserOpenid=%s’, curUserOpenid) eventId = request.args.get("eventId", "") app.logger.debug(‘eventId=%s’, eventId) eventSceneStr = "EVENT_" + eventId app.logger.debug(‘eventSceneStr=%s’, eventSceneStr) postJson = {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": eventSceneStr}}} # postJson = {‘action_info’: {‘scene’: {‘scene_str’: u’EVENT_10′}}, ‘action_name’: ‘QR_LIMIT_STR_SCENE’} app.logger.debug(‘postJson=%s’, postJson) respQrcodeJson = wechat.create_qrcode(postJson) app.logger.debug(‘respQrcodeJson=%s’, respQrcodeJson) # respQrcodeJson = {u’url’: u’http://weixin.qq.com/q/BkRzhBPl4GGZ0mA_UGx3′, # u’ticket’: u’gQGo8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0JrUnpoQlBsNEdHWjBtQV9VR3gzAAIEOxTAVwMEAAAAAA==’} respTicket = respQrcodeJson[‘ticket’] app.logger.debug(‘respTicket=%s’, respTicket) respQrcode = wechat.show_qrcode(respTicket) app.logger.debug(‘type(respQrcode)=%s, respQrcode=%s’, type(respQrcode), respQrcode) respQrcodeContent = respQrcode.content #app.logger.debug(‘type(respQrcodeContent)=%s, respQrcodeContent=%s’, type(respQrcodeContent), respQrcodeContent) app.logger.debug(‘type(respQrcodeContent)=%s’, type(respQrcodeContent)) # save qrcode data to tmp file curTimestamp = time.time() app.logger.debug("type(curTimestamp)=%s, curTimestamp=%s", type(curTimestamp), curTimestamp) qrcodeFullPath = "/tmp/%.3f.jpg" % (curTimestamp) app.logger.debug("qrcodeFullPath=%s", qrcodeFullPath) with open(qrcodeFullPath, ‘wb’) as f: f.write(respQrcodeContent) f.close() app.logger.debug("saved qrcode image to %s ok", qrcodeFullPath) with open(qrcodeFullPath, ‘rb’) as qrcodeFile: app.logger.debug("type(qrcodeFile)=%s, qrcodeFile=%s", type(qrcodeFile), qrcodeFile) uploadMediaResp = wechat.upload_media(media_type="image", media_file=qrcodeFile) app.logger.debug("uploadMediaResp=%s", uploadMediaResp) # uploadMediaResp = {u’media_id’: u’6-_Mx74k2P7B6cmOcbSlyZHkqTQbLj9y-CXUlPY3ltxGckEvbj3YT8fKx2dkNE2X’, # u’created_at’: 1472215847, u’type’: u’image’} qrcodeFile.close() mediaId = uploadMediaResp[‘media_id’] app.logger.debug("mediaId=%s", mediaId) sendImgMsgResp = wechat.send_image_message(user_id=curUserOpenid, media_id=mediaId) app.logger.debug("sendImgMsgResp=%s", sendImgMsgResp) return redirect(url_for(‘index’, curUserOpenId=curUserOpenid)) |
log:
<div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:418]: requestMethod=POST, requestArgs=ImmutableMultiDict([(‘eventId’, u’1′), (‘curUserOpenid’, u’oswjmv4X0cCXcfkIwjoDfCkeTVVY’)]) <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:421]: curUserOpenid=oswjmv4X0cCXcfkIwjoDfCkeTVVY <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:424]: eventId=1 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:427]: eventSceneStr=EVENT_1 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:431]: postJson={‘action_info’: {‘scene’: {‘scene_str’: u’EVENT_1′}}, ‘action_name’: ‘QR_LIMIT_STR_SCENE’} <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:433]: respQrcodeJson={u’url’: u’http://weixin.qq.com/q/E0SC2iHl7WGUq3WcoWx3′, u’ticket’: u’gQGo8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UwU0MyaUhsN1dHVXEzV2NvV3gzAAIEPD/AVwMEAAAAAA==’} <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:438]: respTicket=gQGo8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UwU0MyaUhsN1dHVXEzV2NvV3gzAAIEPD/AVwMEAAAAAA== <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:440]: type(respQrcode)=<class ‘requests.models.Response’>, respQrcode=<Response [200]> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:443]: type(respQrcodeContent)=<type ‘str’> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:447]: type(curTimestamp)=<type ‘float’>, curTimestamp=1472216892.53 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:449]: qrcodeFullPath=/tmp/1472216892.535.jpg <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:453]: saved qrcode image to /tmp/1472216892.535.jpg ok <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:456]: type(qrcodeFile)=<type ‘file’>, qrcodeFile=<open file ‘/tmp/1472216892.535.jpg’, mode ‘rb’ at 0x7ff4478dc030> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:458]: uploadMediaResp={u’media_id’: u’C5y9ZfW0n1W0XS0QeySTyoHqWSUeKRQAR3hyCSfM5RADCOx3RDxAqqwlklFQq20R’, u’created_at’: 1472216892, u’type’: u’image’} <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:464]: mediaId=C5y9ZfW0n1W0XS0QeySTyoHqWSUeKRQAR3hyCSfM5RADCOx3RDxAqqwlklFQq20R <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:466]: sendImgMsgResp={u’errcode’: 0, u’errmsg’: u’ok’} |
即可实现,发送验证码图片给用户:
点击打开图片,长按图片,识别图中二维码:
然后微信服务器收到了对应的事件:
<div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:64]: signature=722759501b865c7310800b4d7f380d72e1aef246, timestamp=1472217198, nonce=1696752715, echostr= <div–<—————————————————————————— tmp_list= [‘sipevents’, u’1472217198′, u’1696752715′] after sort: tmp_list= [u’1472217198′, u’1696752715′, ‘sipevents’] tmp_str= 14722171981696752715sipevents tmp_str_encoded= 14722171981696752715sipevents tmp_str_sha1= <sha1 HASH object @ 0x7ff44795b030> tmp_str_hexdigest= 722759501b865c7310800b4d7f380d72e1aef246 signature= 722759501b865c7310800b4d7f380d72e1aef246 signature OK <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:68]: wechat check_signature OK <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:76]: requestMethod=POST <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:79]: requestData=<xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1472217198</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[SCAN]]></Event> <EventKey><![CDATA[EVENT_10]]></EventKey> <Ticket><![CDATA[gQGo8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0JrUnpoQlBsNEdHWjBtQV9VR3gzAAIEOxTAVwMEAAAAAA==]]></Ticket> </xml> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:86]: requestArgs=ImmutableMultiDict([(‘nonce’, u’1696752715′), (‘timestamp’, u’1472217198′), (‘signature’, u’722759501b865c7310800b4d7f380d72e1aef246′), (‘openid’, u’oswjmv4X0cCXcfkIwjoDfCkeTVVY’)]) <div–<—————————————————————————— data= <xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1472217198</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[SCAN]]></Event> <EventKey><![CDATA[EVENT_10]]></EventKey> <Ticket><![CDATA[gQGo8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0JrUnpoQlBsNEdHWjBtQV9VR3gzAAIEOxTAVwMEAAAAAA==]]></Ticket> </xml> self.conf.encrypt_mode= normal <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:98]: parse post body data OK <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:113]: messageId=0, messageTarget=gh_ac090a9873a8, messageSource=oswjmv4X0cCXcfkIwjoDfCkeTVVY, messageTime=1472217198, messageType=scan, messageRaw=<xml><ToUserName><![CDATA[gh_ac090a9873a8]]></ToUserName> <FromUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></FromUserName> <CreateTime>1472217198</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[SCAN]]></Event> <EventKey><![CDATA[EVENT_10]]></EventKey> <Ticket><![CDATA[gQGo8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0JrUnpoQlBsNEdHWjBtQV9VR3gzAAIEOxTAVwMEAAAAAA==]]></Ticket> </xml> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:151]: respContent=暂时不支持此操作: scan <div–<—————————————————————————— text response= <xml> <ToUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></ToUserName> <FromUserName><![CDATA[gh_ac090a9873a8]]></FromUserName> <CreateTime>1472217198</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[暂时不支持此操作: scan]]></Content> </xml> <div–<—————————————————————————— DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:153]: respContentXml= <xml> <ToUserName><![CDATA[oswjmv4X0cCXcfkIwjoDfCkeTVVY]]></ToUserName> <FromUserName><![CDATA[gh_ac090a9873a8]]></FromUserName> <CreateTime>1472217198</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[暂时不支持此操作: scan]]></Content> </xml> |
暂时此处不支持操作而已:
转载请注明:在路上 » [已解决]微信后台服务器如何发送图片给微信公众号的关注者用户