github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/dkf/session/sesson.go (about)

     1  // 多客服会话控制
     2  package session
     3  
     4  import (
     5  	"net/url"
     6  
     7  	"github.com/chanxuehong/wechat/mp/core"
     8  )
     9  
    10  // Create 创建会话.
    11  //
    12  //	openId:    必须, 客户openid
    13  //	kfAccount: 必须, 完整客服账号,格式为:账号前缀@公众号微信号
    14  //	text:      可选, 附加信息,文本会展示在客服人员的多客服客户端
    15  func Create(clt *core.Client, openId, kfAccount, text string) (err error) {
    16  	const incompleteURL = "https://api.weixin.qq.com/customservice/kfsession/create?access_token="
    17  
    18  	request := struct {
    19  		KfAccount string `json:"kf_account"`
    20  		OpenId    string `json:"openid"`
    21  		Text      string `json:"text,omitempty"`
    22  	}{
    23  		KfAccount: kfAccount,
    24  		OpenId:    openId,
    25  		Text:      text,
    26  	}
    27  	var result core.Error
    28  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    29  		return
    30  	}
    31  	if result.ErrCode != core.ErrCodeOK {
    32  		err = &result
    33  		return
    34  	}
    35  	return
    36  }
    37  
    38  // Close 关闭会话.
    39  //
    40  //	openId:    必须, 客户openid
    41  //	kfAccount: 必须, 完整客服账号,格式为:账号前缀@公众号微信号
    42  //	text:      可选, 附加信息,文本会展示在客服人员的多客服客户端
    43  func Close(clt *core.Client, openId, kfAccount, text string) (err error) {
    44  	const incompleteURL = "https://api.weixin.qq.com/customservice/kfsession/close?access_token="
    45  
    46  	request := struct {
    47  		KfAccount string `json:"kf_account"`
    48  		OpenId    string `json:"openid"`
    49  		Text      string `json:"text,omitempty"`
    50  	}{
    51  		KfAccount: kfAccount,
    52  		OpenId:    openId,
    53  		Text:      text,
    54  	}
    55  	var result core.Error
    56  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    57  		return
    58  	}
    59  	if result.ErrCode != core.ErrCodeOK {
    60  		err = &result
    61  		return
    62  	}
    63  	return
    64  }
    65  
    66  type Session struct {
    67  	OpenId     string `json:"openid"`     // 客户openid
    68  	KfAccount  string `json:"kf_account"` // 正在接待的客服,为空表示没有人在接待
    69  	CreateTime int64  `json:"createtime"` // 会话接入的时间
    70  }
    71  
    72  // Get 获取客户的会话
    73  func Get(clt *core.Client, openId string) (ss *Session, err error) {
    74  	incompleteURL := "https://api.weixin.qq.com/customservice/kfsession/getsession?openid=" +
    75  		url.QueryEscape(openId) + "&access_token="
    76  
    77  	var result struct {
    78  		core.Error
    79  		Session
    80  	}
    81  	if err = clt.GetJSON(incompleteURL, &result); err != nil {
    82  		return
    83  	}
    84  	if result.ErrCode != core.ErrCodeOK {
    85  		err = &result.Error
    86  		return
    87  	}
    88  	result.Session.OpenId = openId
    89  	ss = &result.Session
    90  	return
    91  }
    92  
    93  // List 获取客服的会话列表, 开发者可以通过本接口获取某个客服正在接待的会话列表.
    94  func List(clt *core.Client, kfAccount string) (list []Session, err error) {
    95  	// TODO
    96  	//	incompleteURL := "https://api.weixin.qq.com/customservice/kfsession/getsessionlist?kf_account=" +
    97  	//		url.QueryEscape(kfAccount) + "&access_token="
    98  	incompleteURL := "https://api.weixin.qq.com/customservice/kfsession/getsessionlist?kf_account=" +
    99  		kfAccount + "&access_token="
   100  
   101  	var result struct {
   102  		core.Error
   103  		SessionList []Session `json:"sessionlist"`
   104  	}
   105  	if err = clt.GetJSON(incompleteURL, &result); err != nil {
   106  		return
   107  	}
   108  	if result.ErrCode != core.ErrCodeOK {
   109  		err = &result.Error
   110  		return
   111  	}
   112  	for i := 0; i < len(result.SessionList); i++ {
   113  		result.SessionList[i].KfAccount = kfAccount
   114  	}
   115  	list = result.SessionList
   116  	return
   117  }
   118  
   119  type WaitCaseListResult struct {
   120  	TotalCount int       `json:"count"`                  // 未接入会话数量
   121  	ItemCount  int       `json:"item_count"`             // 本次返回的未接入会话列表数量
   122  	Items      []Session `json:"waitcaselist,omitempty"` // 本次返回的未接入会话列表
   123  }
   124  
   125  // WaitCaseList 获取未接入会话列表.
   126  func WaitCaseList(clt *core.Client) (rslt *WaitCaseListResult, err error) {
   127  	const incompleteURL = "https://api.weixin.qq.com/customservice/kfsession/getwaitcase?access_token="
   128  
   129  	var result struct {
   130  		core.Error
   131  		WaitCaseListResult
   132  	}
   133  	if err = clt.GetJSON(incompleteURL, &result); err != nil {
   134  		return
   135  	}
   136  	if result.ErrCode != core.ErrCodeOK {
   137  		err = &result.Error
   138  		return
   139  	}
   140  	result.WaitCaseListResult.ItemCount = len(result.WaitCaseListResult.Items)
   141  	rslt = &result.WaitCaseListResult
   142  	return
   143  }