一、企业微信简介
企业微信是一款由腾讯公司开发的适用于企业内部使用的即时通讯软件。企业微信拥有安全可靠、功能全面、易于使用等特点,不仅支持基本的聊天和通讯功能,也拥有部门管理、消息推送、会议日程等高级功能。同时,企业微信还提供完善的API接口,方便开发者对其进行二次开发。
二、企业微信API文档概览
企业微信API文档提供丰富的接口,包括通讯录管理、消息管理、群聊管理、菜单管理、素材管理等方面。接口使用简单、灵活,并且提供了多种开发语言的SDK供开发者使用。下面我们就来详细介绍其中一些常用API接口。
三、用户管理
用户管理是企业微信API文档中最基础也是使用频率最高的部分。企业微信API文档提供了大量的用户管理接口,包括用户增删改查、成员管理、标签管理等。下面我们就来介绍其中一些常用的接口。
1. 获取部门成员列表
在企业微信中,每个部门都包含若干个成员。我们可以使用以下API接口获取指定部门中的成员列表:
wx.request({
url: 'https://qyapi.weixin.qq.com/cgi-bin/user/simplelist',
method: 'GET',
data: {
access_token: 'ACCESS_TOKEN',
department_id: 'DEPARTMENT_ID',
},
success: function(res){
console.log(res.data),
},
fail: function(){
console.log('request failed'),
}
})
请求参数中,access_token为企业微信的接口调用凭证,DEPARTMENT_ID为需要获取成员列表的部门id。调用成功后会返回该部门中的成员id和姓名等信息。
2. 创建成员
创建成员接口可以通过调用企业微信API在指定部门中添加新的成员。这是一个POST请求,并且需要指定必须的用户信息。
wx.request({
url: 'https://qyapi.weixin.qq.com/cgi-bin/user/create',
method: 'POST',
data: {
access_token: 'ACCESS_TOKEN',
userid: 'USERID',
name: 'NAME',
department: [DEPARTMENT_ID1,DEPARTMENT_ID2],
position: 'POSITION',
mobile: 'MOBILE',
email: 'EMAIL',
isleader: 0,
enable: 1,
avatar_mediaid: 'MEDIA_ID',
extattr: {
attrs:[
{
name: 'attr1',
value: 'value1'
},
{
name: 'attr2',
value: 'value2'
}
]
}
},
success: function(res){
console.log(res.data),
},
fail: function(){
console.log('request failed'),
}
})
调用成功返回result=0,表示创建成员成功。
四、群聊管理
企业微信API文档提供了丰富的群聊管理接口,包括群聊创建、修改、删除、群组成员管理等。下面我们介绍其中一些常用接口。
1. 创建群聊
我们可以使用以下API接口在企业微信中创建新的群聊:
wx.request({
url: 'https://qyapi.weixin.qq.com/cgi-bin/appchat/create',
method: 'POST',
data: {
access_token: 'ACCESS_TOKEN',
chatid: 'CHAT_ID',
name: 'GROUP_NAME',
owner: 'OWNER_ID',
userlist: [USER1, USER2, ... ],
chat_type: 2
},
success: function(res){
console.log(res.data),
},
fail: function(){
console.log('request failed'),
}
})
请求参数中,CHAT_ID为群聊ID,可以使用随机函数生成;GROUP_NAME为群聊名称;OWNER_ID为群主ID;USER1、USER2等为成员ID列表。调用成功会返回该聊天群的ID和群聊信息。
2. 获取群聊列表
我们可以使用以下API接口获取企业微信中所有的群聊列表:
wx.request({
url: 'https://qyapi.weixin.qq.com/cgi-bin/appchat/list',
method: 'GET',
data: {
access_token: 'ACCESS_TOKEN',
},
success: function(res){
console.log(res.data),
},
fail: function(){
console.log('request failed'),
}
})
调用成功会返回企业微信中所有群聊的ID和名称等信息。
五、消息管理
企业微信API文档提供了丰富的消息管理接口,包括发送消息、接收消息、消息模版等。下面我们介绍其中一些常用接口。
1. 发送应用消息
我们可以使用以下API接口向企业微信中指定的用户或部门发送应用消息:
wx.request({
url: 'https://qyapi.weixin.qq.com/cgi-bin/message/send',
method: 'POST',
data: {
access_token: 'ACCESS_TOKEN',
touser: 'USER_ID_1|USER_ID_2',
toparty: 'PARTY_ID_1|PARTY_ID_2',
totag: 'TAG_ID_1|TAG_ID_2',
msgtype: 'text',
agentid: 'AGENT_ID',
text: {
content: 'message content'
},
safe: 0
},
success: function(res){
console.log(res.data),
},
fail: function(){
console.log('request failed'),
}
})
请求参数中,USER_ID_1|USER_ID_2、PARTY_ID_1|PARTY_ID_2、TAG_ID_1|TAG_ID_2分别为消息接收人员的ID、部门ID和标签ID列表;AGENT_ID为发送应用的ID;content为消息内容。调用成功后返回生产的消息ID。
2. 接收消息
使用企业微信API,我们可以建立应用与企业微信服务器之间的消息交互链路,使应用可以接收到企业微信用户的消息,可以做出相应的处理。
const http = require('http');
const crypto = require('crypto');
const validate_url = 'https://qyapi.weixin.qq.com/cgi-bin/msg_signature';
const token = 'TOKEN';
http.createServer(function(request, response){
if(request.method == 'GET'){
var query = require('url').parse(request.url).query;
var params = {};
query.split('&').forEach(function(item,index){
var param = item.split('=');
params[param[0]] = param[1];
});
var signature = params.signature;
var timestamp = params.timestamp;
var nonce = params.nonce;
var echostr = params.echostr;
var sha1 = crypto.createHash('sha1');
var arr = [token, timestamp, nonce];
arr.sort();
sha1.update(arr.join(''));
if(sha1.digest('hex') === signature){
response.end(echostr);
}
else{
console.log('signature check failed');
response.end();
}
}
else if(request.method == 'POST'){
var postdata = '';
request.addListener('data', function(data){
postdata += data;
});
request.addListener('end', function(){
console.log(postdata);
response.end('success');
});
}
}).listen(8080);
使用上面的代码可以建立一个本地服务器,企业微信服务器会在用户发送消息时向该服务器发送一个POST请求,请求中包含了消息内容。我们可以在代码中对消息进行处理,并且返回“success”表示消息接收成功。
六、素材管理
企业微信API文档提供了丰富的素材管理接口,包括上传素材、下载素材、删除素材等。下面我们介绍其中一些常用的接口。
1. 上传永久图文素材
企业微信中的素材可以用于丰富图文消息的内容。我们可以使用以下API接口上传永久图文素材:
wx.request({
url: 'https://qyapi.weixin.qq.com/cgi-bin/material/add_news',
method: 'POST',
data: {
access_token: 'ACCESS_TOKEN',
articles: [
{
title: 'title1',
thumb_media_id: 'MEDIA_ID_1',
author: 'author1',
content_source_url: '',
content: 'content1',
},
{
title: 'title2',
thumb_media_id: 'MEDIA_ID_2',
author: 'author2',
content_source_url: '',
content: 'content2',
}
]
},
success: function(res){
console.log(res.data),
},
fail: function(){
console.log('request failed'),
}
})
请求参数中,articles为上传的图文素材列表,包含标题、缩略图素材ID、作者、正文来源URL和正文内容等信息。调用成功后返回素材ID。
2. 获取素材列表
我们可以使用以下API接口获取企业微信中所有的素材列表:
wx.request({
url: 'https://qyapi.weixin.qq.com/cgi-bin/material/batchget',
method: 'POST',
data: {
access_token: 'ACCESS_TOKEN',
type: 'news',
offset: 0,
count: 10
},
success: function(res){
console.log(res.data),
},
fail: function(){
console.log('request failed'),
}
})
请求参数中,type为素材的类型,offset为素材列表的起始偏移量,count为获取数量,默认值为20。调用成功后返回素材总数量和素材列表。
