aardio 调用飞书 api 收发消息测试

注1:源码中将密钥之类的隐私信息去除了,所以需要配置一下 config.aardio 后才能使用
注2:发出来仅作记录,其功能比较简单

功能点:检测指定目录下的文件是否有更新,有更新则自动发送至飞书指定群聊/人

aardio 调用飞书接口代码(获取 access token、上传文件、下载文件、获取群消息):

//飞书接口
namespace feishu
import web.json;
import inet.http;
import web.rest.jsonLiteClient;
//import console.int;
//import console.progress;

class FeiShu{
	ctor( appId, appSecret ){
		this.app_id = appId;
		this.app_secret = appSecret;
	};
	getAccessToken = function(url){
		var result;
		
		//创建 HTTP 客户端对象
		var http = ..inet.http(); 
		var response = http.post(url, {
			app_id = this.app_id;
			app_secret = this.app_secret;
		});
		response_data = ..web.json.parse(response);
		if(0 == response_data["code"]){
			result =  response_data["tenant_access_token"];
		}
		else {
			result =  false;
		}
		http.close();
		return result;
	}
	uploadFile = function(url, f_name, f_path, access_token){
		var result;
		
		//创建 REST 客户端
		var http = ..web.rest.client();
		
		//声明 HTTP API
		var ftEllo = http.api(url);
		
		//设置请求头
		headers = {"Authorization" = "Bearer " + access_token};
		http.setHeaders(headers);
		
		//进度条
		//var bar = ..console.progress();
		//bar.doneText = f_name + "-上传完成。";
		
		var response = ftEllo.sendMultipartForm( {
				file_type = "stream";
				file_name = f_name;
				file = f_path; 
			},function(sendData,sendSize,contentLength,remainSize){
				/*
				sendData 为本次上传数据;
				sendSize 为本次上传字节数;
				contentLength 为要上传的总字节数;
				remainSize 为剩余字节数;
				*/
        		//bar.setProgress((1 - remainSize/contentLength) * 100,"正在上传 ......"); 
    		}
		);
		
		if(200 == http.lastStatusCode && null != response){
			//{"code":0,"data":{"file_key":"file_v3_009o_e56a00e1-4670-4620-9392-12572b197c1g"},"msg":"success"}
			response_data = ..web.json.parse(response);
			result = response_data[["data"]][["file_key"]];
			..logger.info("文件【"++file_name++"】已上传,file_key="++result);
		}
		else {
			..logger.error(http.lastResponseString());
			result = false;
		}
		http.close();
		return result;
	}
	downloadFile = function(url, message_id, file_key, file_name, save_path, access_token){
		//创建 REST 客户端
		var http = ..web.rest.client();
		
		//声明 HTTP API
		var ftEllo = http.api(url++"/"++message_id++"/resources/"++file_key);
		
		//设置请求头
		headers = {
			"Authorization" = "Bearer " + access_token;
		};
		http.setHeaders(headers);
		
		//进度条
		//var bar = ..console.progress();
		//bar.doneText = file_name + "-下载完成。";
		
		var recvSizeAll = 0;
		ftEllo.receiveFile(file_name,function(recvData,recvSize,contentLength){
			/*
			recvData 为当前下载数据。
			recvSize 为当前下载数据字节数。
			contentLength 为需要下载的总字节数。
			*/
			//recvSizeAll += recvSize;
			//bar.setProgress((1 - (contentLength - recvSizeAll)/contentLength) * 100,file_name++"-正在下载 ......"); 
		}, save_path).get({
			type = "file";
		});
		http.close();
		..logger.info("文件【"++file_name++"】已下载。");
		return http.lastStatusCode;	
	}
	getChatHistoryMsg = function(url, container_id, page_size, access_token){
		/*
		container_id 容器的ID,可填写:
							chat_id,群聊或单聊的 ID,参见群ID 说明
							thread_id,话题 ID,参见话题介绍
							示例值:"oc_234jsi43d3ssi993d43545f"
		page_size 分页大小
		*/
		var result;
		
		//创建 REST 客户端
		var http = ..web.rest.client();
		
		//声明 HTTP API
		var ftEllo = http.api(url);
		
		//设置请求头
		headers = {
			"Authorization" = "Bearer " + access_token;
		};
		http.setHeaders(headers);
		
		var response = ftEllo.get({
			container_id_type = "chat";
			container_id = container_id;
			sort_type = "ByCreateTimeDesc";
			page_size = page_size;
		})
		
		if(200 == http.lastStatusCode && null != response){
			response_data = ..web.json.parse(response);
			result = response_data[["data"]][["items"]];
		}
		else {
			..logger.error(http.lastResponseString());
			result = false;
		}
		
		http.close();
		return result;
	}
	
}

完整工程代码:

https://gitea.iioio.com:3000/aardio/Feishu-api-test

© 版权声明
THE END
喜欢就支持一下吧
点赞7赞赏 分享
评论 共2条
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片
    • 头像send your cv0