侧边栏JS-SDK

侧边栏JS-SDK是面向网页开发者提供的的网页开发工具包,类似企业微信JS-SDK。

使用说明

在需要调用JS接口的页面引入JS文件,通过wx对象调用相关接口。

<script src="//res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script src="https://cdn.botorange.com/js/sidebar/juzi-helper-1.0.8.js"></script>
<!-- 
  1.juzi-helper仅在句子秒回侧边栏环境下覆写wx对象,成功后window.wx.isJuziWx === true
  2.如果使用npm安装的jweixin或页面不存在wx对象,juzi-helper的覆写可能不会成功,此时可使用window.juziWx
  -->

相关接口

获取当前外部联系人userid

同企业微信JS-SDK

wx.invoke('getCurExternalContact', { }, function(res){
  if (res.err_msg == "getCurExternalContact:ok") {
    //成功处理 {userId: 'xxx', err_msg: 'getCurExternalContact:ok'}
  } else {
    //错误处理 {userId: undefined, err_msg: 'getCurExternalContact:fail'}
  }
});

获取当前客户群的群ID

从客户群的聊天工具栏进入页面时才可成功调用该接口

wx.invoke('getCurExternalChat', {}, function(res){
  if (res.err_msg == "getCurExternalChat:ok") {
    //成功处理 {chatId: 'xxx', err_msg: 'getCurExternalChat:ok'}
  } else {
    //错误处理 {chatId: undefined, err_msg: 'getCurExternalChat:fail'}
  }
});

获取当前聊天会话信息

wx.getCurChatInfo({
  success: function(res) {
    // 成功处理
  },
  fail: function(res) {
    // 失败处理 {err_msg: 'xxx'}
  }
})

success 返回对象的扩展属性如下所示:

名称数据类型描述
Idstring会话id。
namestring会话名称。
typenumber会话类型。0 单聊,1群聊。
externalboolean是否是外部的。true 外部的,false 内部的。
wxUserIdstring企业微信联系人userId。
可选,当type为 0 时有。
等同 getCurExternalContact 接口拿到的userId。
wxChatIdstring企业微信群聊chatId。
可选,当type为 1 时有。
等同 getCurExternalChat 接口拿到的chatId。
botIdstring托管账号ID。
botNamestring托管账号名称。
botStatusnumber托管账号状态。1离线, 2在线。
botTypenumber托管账号类型。
export enum BotType {
BOTTYPE_NONE = 0,
WECHAT = 1,
WXWORK = 2,
OA = 3,
WHATSAPP = 4,
}
jzGroupIdstring句子小组ID。
jzUserIdstring句子用户(右上角用户)的ID。
注意:非托管账号用户ID, 托管账号用户ID请参考"身份认证"。

分享消息到当前会话

type TextMsg = { msgtype: 'text'; text: { content: string; } };

type ImageMsg = { 
  msgtype: 'image';
  image: { 
    url: string;
    title?: string;
  };
}

type VideoMsg = { 
  msgtype: 'video';
  video: { 
    url: string;
    title?: string;
  };
}

type FileMsg = { 
  msgtype: 'file';
  file: { 
    url: string;
    title?: string; 
  };
}

type NewsMsg = {
  msgtype: 'news',
  news: {
    link: string,
    title?: string,
    desc?: string,
    imgUrl?: string,
  },
}

type MiniprogramMsg = {
  msgtype: "miniprogram",
  miniprogram: {
    appid: string,    // 小程序的appid
    title: string,    // 小程序消息的title
    imgUrl: string,   // 小程序消息的封面图。必须带http或者https协议头
    page: string,     // 小程序消息打开后的路径
    desc: string,     // 小程序描述
    iconUrl: string,  // 小程序icon地址
    officialAccountId: string,    // 小程序原始id
  },
}

type SendParams = TextMsg | ImageMsg | VideoMsg | FileMsg | NewsMsg | MiniprogramMsg;

type ErrMsg = 'sendChatMessage:ok' | 'text content is empty' | 'url is empty';

type Callback = (res: { err_msg: ErrMsg; }) => void;

wx.invoke('sendChatMessage', sendParams: SendParams, callback: Callback);

打开会话

调用接口将会在【聚合聊天-全部】中打开会话

wx.openEnterpriseChat({
  userIds: 'zhangsan',    // 企业微信企业成员userId,必传
  externalUserIds: 'wmEAlECwAAHrbWYDOK5u3Bf13xlYDAAA', // 企业微信外部联系人userId
  chatId: 'CHATID', // 企业微信外部群chatId
  success: function(res) {
    // 成功处理 {err_msg: 'openEnterpriseChat:ok'}
  },
  fail: function(res) {
    // 失败处理 {err_msg: 'xxx'}
  }
})

TIP

仅新版聚合聊天支持,旧版聚合聊天不支持此接口

仅支持打开会话,暂不支持新建会话

externalUserIds 和 chatId 二选一,externalUserIds优先于chatId

预览图片

wx.previewImage({
  current: '',  // 可选,第一张显示的图片链接
  urls: [],     // 必填,需要预览的图片http链接列表
})

TIP

仅新版聚合聊天支持,旧版聚合聊天不支持此接口

示例代码

这是个可在线运行的 demo,使用方式如下:

  1. 复制地址:https://docs.juzibot.com/sidebar-demo.html,或者拷贝sidebar-demo.html的源码到本地页面。
  2. 配置到句子聊天工具栏。前往「企业控制台-配置中心-聚合聊天工具栏」配置, 或前往「聊天工作台-设置-自定义侧边栏」配置。
  3. 前往「聊天工作台-聚合聊天 」使用并调试。

身份认证

提供了OAuth的授权登录方式,可以让从自定义侧边栏打开的网页获取成员的身份信息,从而免去登录的环节。

构造授权链接

如果企业需要在打开的网页里面携带用户的身份信息,第一步需要构造如下的链接来获取code参数:

https://hub.juzibot.com/oauth/connect/authorize?redirect_uri=REDIRECT_URI&state=STATE

参数说明:

参数必须说明
redirect_uri授权后重定向的回调链接地址,请使用urlencode对链接进行处理
state重定向后会带上state参数,企业可以填写a-zA-Z0-9的参数值,长度不可超过128个字节

将会以当前聊天窗口所属的托管企业微信进行自动授权,授权后页面将跳转至 redirect_uri?code=CODE&state=STATE&juziBotId=juziBotId,企业可根据code参数获得员工的userId。juziBotId为托管微信的id。

获取成员(托管账号)身份

该接口用于根据code获取成员信息

请求地址:

GET https://hub.juzibot.com/api/v1/oauth/getUserInfo?code=**

参数说明:

参数类型备注是否必须
codestring通过成员授权获取到的code。每次成员授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。

返回示例:

{
   "errcode": 0,
   "errmsg": "",
   "data": {
     "userId":"userId",
     "corpId":"ww5ecc1acd5dce6e9d",
     "botId":"647844cd6059597fc5c79f38",
     "orgId":"644a448ab93794cbdccf93cb",
     "uid": "644a448ab93794cbdccf93ca",
     "email": "donghao_12_15@163.com"
   }
}

返回参数说明:

参数类型必定存在备注
errcodenumber返回码,0 成功,-1 无效的授权码,-2 授权码已过期,-4 未找到授权码,-5 未知错误
errmsgstring对返回码的文本描述内容
data.userIdstring托管账号的企业微信明文userId
data.corpIdstring托管账号的企业微信明文corpId
data.botIdstring托管账号id
data.orgIdstring秒回企业id
data.uidstring秒回成员id
data.emailstring秒回成员邮箱
data.namestring秒回成员姓名
最近更新:
Contributors: iseei, hanyachao, SilentQianyi, fanmenglin