zoukankan      html  css  js  c++  java
  • 自定义菜单和高级接口-自定义菜单

    官方文档

    自定义菜单能够帮助公众号丰富界面,让用户更好更快地理解公众号的功能。

    1. 自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。
    2. 一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。
    3. 创建自定义菜单后,菜单的刷新策略是,在用户进入公众号会话页或公众号profile页时,如果发现上一次拉取菜单的请求在5分钟以前,就会拉取一下菜单,如果菜单有更新,就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果

    自定义菜单接口可实现多种类型按钮:

    1. click: 点击推事件: 微信推消息类型为event的结构给开发者,并带上按钮中开发者填写的key值.开发者可以通过自定义的key值与用户进行交互. 微信公众平台会把此次单击事件推送给接口程序。
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[CLICK]]></Event>
        <EventKey><![CDATA[EVENTKEY]]></EventKey>
      </xml>
    2. view: 跳转URL. 微信客户端打开开发者在按钮中填写的网页URL.  微信公众平台不会上报单击事件,而是直接跳转到对应的URL.
    3. scancode_push: 扫码推事件。 微信客户端将调用扫一扫工具,完成扫码操作后显示扫码结果(如果是URL,将进入URL)
      接口程序会收到XML数据包,但用户收不到这个消息,而是直接运行解码后的内容
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[scancode_push]]></Event>
        <EventKey><![CDATA[EVENTKEY]]></EventKey>
        <ScanCodeInfo>
            <ScanType><![CDATA[qrcode]]></ScanType>
          <ScanResult><![CDATA[http://www.baidu.com]]></ScanResult>
          <EventKey><![CDATA[EVENTKEY]]></EventKey>
        </ScanCodeInfo>
      </xml>
    4. scancode_waitmsg: 扫码推事件且弹出 ‘消息接收中’的提示框
      微信客户端将调用扫一扫工具,完成扫码操作后,将扫码的结果传送给开发者,同时收起‘扫一扫’功能,然后弹出‘消息接收中’的提示框
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[scancode_waitmsg]]></Event>
        <EventKey><![CDATA[EVENTKEY]]></EventKey>
        <ScanCodeInfo>
            <ScanType><![CDATA[qrcode]]></ScanType>
          <ScanResult><![CDATA[http://www.baidu.com]]></ScanResult>
          <EventKey><![CDATA[EVENTKEY]]></EventKey>
        </ScanCodeInfo>
      </xml>
    5. pic_sysphoto: 弹出系统拍照发图。 将拍摄的照片发送给开发者,并推送事件给开发者。
      单击这个类型的菜单后,没有XML数据推送过来,而拍照以后再发过来时,就收到一个图片消息:(推送什么样的事件消息,后面验证后再贴图)
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>1348831860</CreateTime>
        <MsgType><![CDATA[image]]></MsgType>
        <PicUrl><![CDATA[this is a url]]></PicUrl>
        <MediaId><![CDATA[media_id]]></MediaId>
        <MsgId>1234567890123456</MsgId>
      </xml>
    6. pic_photo_or_album:弹出拍照或相册发图:
      先推送事件,再推送图片消息
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[pic_photo_or_album]]></Event>
        <EventKey><![CDATA[EVENTKEY]]></EventKey>
        <SendPicsInfo>
            <Count><![CDATA[1]]></Count>
            <PicList>
              <item>
                  <PicMd5Sum><![CDATA[58f4xxxxxxxxxx]]></PicMd5Sum>
              </item>
            </PicList>
        </SendPicsInfo>
      </xml>
      
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>1348831860</CreateTime>
        <MsgType><![CDATA[image]]></MsgType>
        <PicUrl><![CDATA[this is a url]]></PicUrl>
        <MediaId><![CDATA[media_id]]></MediaId>
        <MsgId>1234567890123456</MsgId>
      </xml>
    7. pic_weixin:弹出微信相册发图
      先推送事件,再推送图片消息
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[pic_weixin]]></Event>
        <EventKey><![CDATA[EVENTKEY]]></EventKey>
        <SendPicsInfo>
            <Count><![CDATA[3]]></Count>
            <PicList>
              <item>
                  <PicMd5Sum><![CDATA[58f4xxxxxxxxxx]]></PicMd5Sum>
              </item>
                      <item>
                  <PicMd5Sum><![CDATA[58f4xxxxxxxxxx]]></PicMd5Sum>
              </item>
                      <item>
                  <PicMd5Sum><![CDATA[58f4xxxxxxxxxx]]></PicMd5Sum>
              </item>
            </PicList>
        </SendPicsInfo>
      </xml>
      
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>1348831860</CreateTime>
        <MsgType><![CDATA[image]]></MsgType>
        <PicUrl><![CDATA[this is a url]]></PicUrl>
        <MediaId><![CDATA[media_id]]></MediaId>
        <MsgId>123456789012345x</MsgId>
      </xml>
      
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>1348831860</CreateTime>
        <MsgType><![CDATA[image]]></MsgType>
        <PicUrl><![CDATA[this is a url]]></PicUrl>
        <MediaId><![CDATA[media_id]]></MediaId>
        <MsgId>1234567890123456</MsgId>
      </xml>
      
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>1348831860</CreateTime>
        <MsgType><![CDATA[image]]></MsgType>
        <PicUrl><![CDATA[this is a url]]></PicUrl>
        <MediaId><![CDATA[media_id]]></MediaId>
        <MsgId>1234567890123456</MsgId>
      </xml>
       对于上述每个图片的消息,后台收到都能进行单独回应       
    8. location_select: 弹出地理位置选择器。 调用地理位置选择功能。
      在用户发送位置后,会再推送一个地理位置消息功能给用户
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[location_select]]></Event>
        <EventKey><![CDATA[EVENTKEY]]></EventKey>
        <SendLocationInfo>
          <Location_X><![CDATA[23.134521]]></Location_X>
          <Location_Y><![CDATA[113.358803]]></Location_Y>
          <Scale><![CDATA[20]]></Scale>
          <Label><![CDATA[位置信息]]></Label>
          <Poiname><![CDATA[]]></Poiname>
          <EventKey><![CDATA[EVENTKEY]]></EventKey>
        </SendLocationInfo>
      </xml>
      
      <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[fromUser]]></FromUserName>
        <CreateTime>1351776360</CreateTime>
        <MsgType><![CDATA[location]]></MsgType>
        <Location_X>23.134521</Location_X>
        <Location_Y>113.358803</Location_Y>
        <Scale>20</Scale>
        <Label><![CDATA[位置信息]]></Label>
        <MsgId>1234567890123456</MsgId>
      </xml>
    9. media_id: 下发消息(除文本消息)用户点击media_id类型按钮后,微信服务器将开发者填写的永久素材id 对应的素材下发给用户。永久素材类型可以是图片、音频、视频、图文消息。请注意:永久素材id必须是在“素材管理/新增永久素材”接口上传后获得的合法id。
    10. view_limited: 跳转图文消息URL 。微信客户端将打开开发者在按钮中填写的永久素材id对应的图文消息URL,永久素材类型只支持图文消息。请注意:永久素材id必须是在“素材管理/新增永久素材”接口上传后获得的合法id。 ​

    9和10,是专门给第三方平台旗下未微信认证(具体而言,是资质认证未通过)的订阅号准备的事件类型,它们是没有事件推送的,能力相对受限,其他类型的公众号不必使用。

    创建菜单:

      接口: http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

       返回:{"errcode":0,"errmsg":"ok"} 或 {"errcode":40018,"errmsg":"invalid button name size"}

    
    
    def create_menu(access_token,json_menu):
    print(json_menu)
    headers = {
    'content_type': "application/json"
    }
    json_menu = json.dumps(json_menu,ensure_ascii=False)
    create_url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}'.format(access_token)
    response = requests.post(create_url,json_menu.encode('utf-8'),headers=headers)
    return json.loads(response.text)
    注意:这个debug了好久。首先需要将 python的 dict转化为json 数据,然后在传入post 参数时进行 utf-8封装。不然 就是创建不成功,
    总是返回:{'errcode': 40016, 'errmsg': 'invalid button size hint

     menu_data: 

    menu_data = {
        "button": [
            {
                "name": "新手",
                "sub_button": [
                    {
                        "type": "click",
                        "name": "点击拉歌曲",
                        "key": "click_music"
                    },
                    {
                        "type": "view",
                        "name": "查看百度",
                        "url": "http://www.baidu.com"
                    },
                    {
                        "type": "scancode_waitmsg",
                        "name": "扫码等消息",
                        "key": "scancode_waitmsg_event"
                    },
                    {
                        "type": "pic_sysphoto",
                        "name": "系统相册",
                        "key": "pic_sysphoto_event"
                    }
                ]
            },
            {
                "name": "其他",
                "sub_button": [
                    {
                        "type": "scancode_push",
                        "name": "扫码推事件",
                        "key": "scancode_push_event"
                    },
                    {
                        "type": "pic_photo_or_album",
                        "name": "拍照或选照片",
                        "key": "pic_photo_or_album_event"
                    },
                    {
                        "type": "pic_weixin",
                        "name": "微信相册",
                        "key": "pic_weixin_event"
                    },
                    {
                        "type": "location_select",
                        "name": "发送位置",
                        "key": "location_select_event"
                    }
                ]
            }
        ]
    }

    查询菜单:

      接口: http请求方式: GET(请使用https协议)https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=ACCESS_TOKEN

       返回:https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Querying_Custom_Menus.html

    alue、url、key等字段    对于不同的菜单类型,value的值意义不同。官网上设置的自定义菜单: Text:保存文字到value; Img、voice:保存mediaID到value;
                 Video:保存视频下载链接到value; News:保存图文消息到news_info,同时保存mediaID到value; View:保存链接到url。
                使用API设置的自定义菜单: click、scancode_push、scancode_waitmsg、pic_sysphoto、pic_photo_or_album、
                 pic_weixin、location_select:保存值到key;view:保存链接到url
    def query_menu(access_token):
        query_url = 'https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=%s'% access_token
        response = requests.get(query_url)
        return json.loads(response.content)

    删除菜单:

       接口: http请求方式:GET https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN

       返回:对应创建接口,正确的Json返回结果:{"errcode":0,"errmsg":"ok"} 

      注意: 在个性化菜单时,调用此接口会删除默认菜单及全部个性化菜单。

    def delete_menu(access_token):
        delete_url = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=%s'% access_token
        response = requests.get(delete_url)
        return json.loads(response.text)

    实践是,就算我发送多张图片,接口接收到的消息也是 0个.

  • 相关阅读:
    机器学习——EM算法
    机器学习——贝叶斯分类器
    机器学习——朴素贝叶斯法
    论文解读(SimCLR)《A Simple Framework for Contrastive Learning of Visual Representations》
    机器学习——数据增强
    机器学习——正则化方法Dropout
    ResNet
    机器学习——最小二乘法
    机器学习——K-Means算法
    机器学*——K*邻算法(KNN)
  • 原文地址:https://www.cnblogs.com/ahMay/p/12047826.html
Copyright © 2011-2022 走看看