zoukankan      html  css  js  c++  java
  • 高级接口--客服接口

    官方文档

    当用户和公众号产生特定动作的交互时(具体动作列表请见下方说明),微信将会把消息数据推送给开发者,开发者可以在一段时间内(目前修改为48小时)调用客服接口,通过POST一个JSON数据包来发送消息给普通用户。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。

    允许的动作:

    1、用户发送信息
    2、点击自定义菜单(仅有点击推事件、扫码推事件、扫码推事件且弹出“消息接收中”提示框这3种菜单类型是会触发客服接口的)
    3、关注公众号
    4、扫描二维码
    5、支付成功
    6、用户维权

    客服帐号管理

    开发者在根据开发文档的要求完成开发后,使用6.0.2版及以上版本的微信用户在与公众号进行客服沟通,公众号使用不同的客服账号进行回复后,用户可以看到对应的客服头像和昵称。

    请注意,必须先在公众平台官网为公众号设置微信号后才能使用该能力。

    media    是    该参数仅在设置客服头像时出现,是form-data中媒体文件标识,有filename、filelength、content-type等信息
    客服接口
    #https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Service_Center_messages.html
    import requests
    import json
    import wechatBasic
    
    def add_kfaccount(access_token, json_data):
        '''
        {
         "kf_account" : "test1@test",
         "nickname" : "客服1",
         "password" : "pswmd5"
    }
        :param access_token:
        :param json_data:
        :return:
        '''
        kfaccount_add_url = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token=%s'% access_token
        json_data = json.dumps(json_data,ensure_ascii=False)
        response = requests.post(kfaccount_add_url,json_data.encode('utf-8'))
        return json.loads(response.text)
    
    def update_kfaccount(access_token, json_data):
        kfaccount_update_url = 'https://api.weixin.qq.com/customservice/kfaccount/update?access_token=%s'% access_token
        json_data = json.dumps(json_data,ensure_ascii=False)
        response = requests.post(kfaccount_update_url,json_data.encode('utf-8'))
        return json.loads(response.text)
    
    def delete_kfaccount(access_token, json_data):
        kfaccount_delete_url = 'https://api.weixin.qq.com/customservice/kfaccount/del?access_token=%s'% access_token
        json_data = json.dumps(json_data,ensure_ascii=False)
        #官方文档说是GET,但感觉是官方文档错误。
        response = requests.post(kfaccount_delete_url,json_data.encode('utf-8'))
        return json.loads(response.text)
    
    def set_kfaccount_headingimg(access_token,kfaccount,img_path):
        '''
    
        :param access_token:
        :param kfaccount: test1@test1
        :param img_path:
        :return:
        '''
        uploading_url = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=%s&kf_account=%s'%
                        (access_token,kfaccount)
        data ={
            'media':open(img_path,'rb')
        }
        response = requests.post(uploading_url,files=data)
        return json.loads(response.text)
    
    def query_kfaccounts(access_token):
        query_url = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=%s'% access_token
        response = requests.get(query_url)
        return json.loads(response.content)

    客服接口-发消息

    def send_kfmsg(access_token,json_data):
        send_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s'% access_token
        json_data = json.dumps(json_data,ensure_ascii=False)
        response = requests.post(send_url, json_data.encode('utf-8'))
    
        return json.loads(response.text)

    各消息类型所需的JSONs数据包:

      文本消息:

    {
        "touser":"OPENID",
        "msgtype":"text",
        "text":
        {
             "content":"Hello World"
        }
    }

    注意:发送文本消息时,支持插入跳小程序的文字链
    文本内容<a href="http://www.qq.com" data-miniprogram-appid="appid" data-miniprogram-path="pages/index/index">点击跳小程序</a>

    说明: 1.data-miniprogram-appid 项,填写小程序appid,则表示该链接跳小程序; 2.data-miniprogram-path项,填写小程序路径,路径与app.json中保持一致,可带参数; 3.对于不支持data-miniprogram-appid 项的客户端版本,如果有herf项,则仍然保持跳href中的网页链接; 4.data-miniprogram-appid对应的小程序必须与公众号有绑定关系
     

    发送图片消息:

    {
        "touser":"OPENID",
        "msgtype":"image",
        "image":
        {
          "media_id":"MEDIA_ID"
        }
    }

    发送语音消息:

    {
        "touser":"OPENID",
        "msgtype":"voice",
        "voice":
        {
          "media_id":"MEDIA_ID"
        }
    }

    发送视频消息:

    {
        "touser":"OPENID",
        "msgtype":"video",
        "video":
        {
          "media_id":"MEDIA_ID",
          "thumb_media_id":"MEDIA_ID",
          "title":"TITLE",
          "description":"DESCRIPTION"
        }
    }

    发送音乐消息:

    {
        "touser":"OPENID",
        "msgtype":"music",
        "music":
        {
          "title":"MUSIC_TITLE",
          "description":"MUSIC_DESCRIPTION",
          "musicurl":"MUSIC_URL",
          "hqmusicurl":"HQ_MUSIC_URL",
          "thumb_media_id":"THUMB_MEDIA_ID" 
        }
    }
     

    发送图文消息:(发送图文消息(点击跳转到外链) 图文消息条数限制在1条以内,注意,如果图文数超过1,则将会返回错误码45008。)

    {
        "touser":"OPENID",
        "msgtype":"news",
        "news":{
            "articles": [
             {
                 "title":"Happy Day",
                 "description":"Is Really A Happy Day",
                 "url":"URL",
                 "picurl":"PIC_URL"
             }
             ]
        }
    }

    发送图文消息(点击跳转到图文消息页面) 图文消息条数限制在1条以内,注意,如果图文数超过1,则将会返回错误码45008。

    {
        "touser":"OPENID",
        "msgtype":"mpnews",
        "mpnews":
        {
             "media_id":"MEDIA_ID"
        }
    }

    发送菜单消息:

    {
      "touser": "OPENID"
      "msgtype": "msgmenu",
      "msgmenu": {
        "head_content": "您对本次服务是否满意呢? "
        "list": [
          {
            "id": "101",
            "content": "满意"
          },
          {
            "id": "102",
            "content": "不满意"
          }
        ],
        "tail_content": "欢迎再次光临"
      }
    }

    其中,“满意”和“不满意”是可点击的,当用户点击后,微信会发送一条XML消息到开发者服务器,格式如下:

    <xml>
    <ToUserName><![CDATA[ToUser]]></ToUserName>
    <FromUserName><![CDATA[FromUser]]></FromUserName>
    <CreateTime>1500000000</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[满意]]></Content>
    <MsgId>1234567890123456</MsgId>
    <bizmsgmenuid>101</bizmsgmenuid>
    </xml>
    收到XML推送之后,开发者可以根据提取出来的bizmsgmenuidContent识别出微信用户点击的是哪个菜单

    发送卡券消息:

    {
      "touser":"OPENID", 
      "msgtype":"wxcard",
      "wxcard":
      {              
       "card_id":"123dsdajkasd231jhksad"        
       }
    }
    特别注意客服消息接口投放卡券仅支持非自定义Code码和导入code模式的卡券的卡券,详情请见:创建卡券

    发送小程序卡片(要求小程序与公众号已关联):

    {
        "touser":"OPENID",
        "msgtype":"miniprogrampage",
        "miniprogrampage":
        {
            "title":"title",
            "appid":"appid",
            "pagepath":"pagepath",
            "thumb_media_id":"thumb_media_id"
        }
    }

      

    请注意,如果需要以某个客服帐号来发消息(在微信6.0.2及以上版本中显示自定义头像),则需在JSON数据包的后半部分加入customservice参数,例如发送文本消息则改为:

    {
        "touser":"OPENID",
        "msgtype":"text",
        "text":
        {
             "content":"Hello World"
        },
        "customservice":
        {
             "kf_account": "test1@kftest"
        }
    }

    客服输入状态

    开发者可通过调用“客服输入状态”接口,返回客服当前输入状态给用户。

    此接口需要客服消息接口权限。

    1. 如果不满足发送客服消息的触发条件,则无法下发输入状态。

    2. 下发输入状态,需要客服之前30秒内跟用户有过消息交互。

    3. 在输入状态中(持续15s),不可重复下发输入态。

    4. 在输入状态中,如果向用户下发消息,会同时取消输入状态。

    def send_kfstatus(access_token, input_type):
        '''{ "touser":"OPENID", "command":"Typing"}'''
        if input_type not in ('Typing','CancelTyping'):
            raise Exception('输入状态必须是:' + 'Typing' + '或者:' + 'CancelTyping')
        send_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token=%s'% access_token
        json_data = json.dumps(input_type)
        response = requests.post(send_url, json_data)
        return json.loads(response.text)

    由于本人的公众号权限受限,这节内容都没有测试验证

  • 相关阅读:
    CSS基础教程要点笔记
    Python编程入门-第六章 字符串 -学习笔记
    Swift游戏实战-跑酷熊猫 08 产生源源不断的移动平台
    Swift游戏实战-跑酷熊猫 07 平台的移动
    ANE-IOS与AS的互通
    Swift游戏实战-跑酷熊猫 06 创建平台类以及平台工厂类
    Swift游戏实战-跑酷熊猫 05 踩踏平台是怎么炼成的
    构建针对 iOS 和 Android 的原生扩展
    ANE打包心得
    Swift游戏实战-跑酷熊猫 04 熊猫的跳和滚的动作
  • 原文地址:https://www.cnblogs.com/ahMay/p/12054020.html
Copyright © 2011-2022 走看看