362 lines
12 KiB
Plaintext
362 lines
12 KiB
Plaintext
import config;
|
||
import process;
|
||
import fsys.dirWatcher;
|
||
import win.ui;
|
||
//import godking.vlistEx;
|
||
import sqlite.latest;
|
||
import console;
|
||
import time;
|
||
import logger;
|
||
/*DSG{{*/
|
||
var mainForm = win.form(text=config.__appName;right=1088;bottom=652)
|
||
mainForm.add(
|
||
//vlist={cls="vlistEx";left=10;top=8;right=1079;bottom=646;db=1;dl=1;dr=1;dt=1;edge=1;transparent=1;z=1}
|
||
)
|
||
/*}}*/
|
||
|
||
console.setTitle(config.__appName);
|
||
logger = logger.Logger();
|
||
|
||
|
||
import feishu;
|
||
var Transsioner = feishu.FeiShu(config.__feishu.appConfig.app_id, config.__feishu.appConfig.app_secret);
|
||
|
||
mainForm.feishuGetChatMsg = function(msg_count){
|
||
var access_token = Transsioner.getAccessToken(config.__feishu.api.access_token_url);
|
||
//mainForm.info(access_token);
|
||
|
||
//var file_key = Transsioner.uploadFile(config.__feishu.api.file_upload_url, "test.txt", "@F:\ZTools\aardio\test.txt", access_token);
|
||
//console.log(file_key);
|
||
|
||
var chat_history_msg = Transsioner.getChatHistoryMsg(config.__feishu.api.messages_url, "oc_580423c5700a2108036400310c634dcb", msg_count, access_token);
|
||
return chat_history_msg;
|
||
}
|
||
|
||
mainForm.feishuDownloadFile = function(message_id, file_key, file_name){
|
||
var access_token = Transsioner.getAccessToken(config.__feishu.api.access_token_url);
|
||
logger.info(access_token);
|
||
var file_data = Transsioner.downloadFile(config.__feishu.api.messages_url, message_id, file_key, file_name, "./temp", access_token);
|
||
}
|
||
|
||
//==========================================test============================================
|
||
var test = function(){
|
||
var db = sqlite("/sqlite.db"); //创建数据库
|
||
if( not db.existsTable("chat") ){
|
||
db.exec( "CREATE TABLE [chat](create_time, chat_id, message_id, msg_type, body);"); //创建表
|
||
}
|
||
var result = db.stepQuery("SELECT * FROM [chat]",{ create_time = "1712827756523"})
|
||
var data = web.json.parse(result["body"]["content"]);
|
||
//console.dump(data["file_key"]);
|
||
//console.dump(data["file_name"]);
|
||
//mainForm.feishuDownloadFile(result["message_id"], data["file_key"], data["file_name"]);
|
||
|
||
var data = mainForm.feishuDownloadFile("om_3e712824a2a698aef85af022ce5c9dbc", "file_v3_009r_efb97b0b-0b50-41ba-a41e-97af82e0fa9g", "LC2002_it8010_sp.pac");
|
||
|
||
}
|
||
//test();
|
||
|
||
//=========================使用单独的线程操作文件下载、上传、消息提醒=========================================
|
||
var downAndUpFileThread = function(mainForm, data, odm){
|
||
thread.invoke(
|
||
function(mainForm, data, odm){
|
||
import fsys;
|
||
import time;
|
||
import sqlite.latest;
|
||
import feishu;
|
||
import config;
|
||
import logger;
|
||
logger = logger.Logger();
|
||
|
||
var db = sqlite("/sqlite.db");
|
||
var status = false;
|
||
|
||
var feishuWebhookBotSendMsg = function(webhook, msg){
|
||
import inet.http;
|
||
//创建 HTTP 客户端对象
|
||
try{
|
||
var http = ..inet.http();
|
||
var response = http.post(webhook, web.json.stringify({
|
||
msg_type = "text";
|
||
content = {
|
||
text = msg;
|
||
}
|
||
}));
|
||
//{"StatusCode":0,"StatusMessage":"success","code":0,"data":{},"msg":"success"}
|
||
var response_data = web.json.parse(response);
|
||
http.close();
|
||
if(response_data["StatusCode"] == 0){
|
||
logger.success("机器人通知已发出:"++msg);
|
||
return true;
|
||
}
|
||
else {
|
||
logger.error("机器人通知发送失败:"++response);
|
||
return false;
|
||
}
|
||
}
|
||
catch(e){
|
||
logger.error(e);
|
||
}
|
||
}
|
||
|
||
try{
|
||
var Transsioner = feishu.FeiShu(config.__feishu.appConfig.app_id, config.__feishu.appConfig.app_secret);
|
||
var access_token = Transsioner.getAccessToken(config.__feishu.api.access_token_url);
|
||
|
||
var _create_time = data["create_time"];
|
||
var _message_id = data["message_id"];
|
||
var file_data = web.json.parse(data["body"]["content"]);
|
||
var file_key = file_data["file_key"];
|
||
var file_name = file_data["file_name"];
|
||
//mainForm.vlist.addRow("[@rowindex]", _create_time, odm, file_name, "正在下载...");
|
||
//var row_index = mainForm.vlist.find(_create_time,1,mainForm.vlist.count,2,false,false);
|
||
|
||
//下载文件
|
||
var download_to = ".\feishu2ftp\" ++ _create_time;
|
||
var download_result = Transsioner.downloadFile(config.__feishu.api.messages_url, _message_id, file_key, file_name, download_to, access_token);
|
||
if(download_result == 200){
|
||
var file_path_local = download_to ++ "\" ++ file_name;
|
||
if(io.exist(file_path_local)){
|
||
var copy_to = config.__copy2Folder[odm] ++ "\" ++ _create_time;//飞书上的文件复制到服务器该目录下
|
||
var copy_result = fsys.copy(".\feishu2ftp\"++_create_time, copy_to);
|
||
if(copy_result){
|
||
logger.success("新文件已上传至:" ++ copy_to);
|
||
logger.debug("即将调用机器人发送通知,webhook="++config.__feishu.chat_bot_webhook[odm]);
|
||
feishuWebhookBotSendMsg(config.__feishu.chat_bot_webhook[odm], "文件【" ++ file_name ++ "】已放入该路径:" ++ copy_to);
|
||
|
||
//mainForm.vlist.setRowText(row_index, {"序号"="[@rowindex]", "时间"=_create_time, "ODM"=odm, "文件"=file_name, "状态"="已上传"});
|
||
status = true;
|
||
}
|
||
}
|
||
else {
|
||
logger.error("本地临时目录未找到下载的文件:" ++ file_path_local);
|
||
}
|
||
}
|
||
else {
|
||
logger.error("文件下载失败。")
|
||
//mainForm.vlist.setRowText(row_index, {"序号"="[@rowindex]", "时间"=_create_time, "ODM"=odm, "文件"=file_name, "状态"="文件下载失败"});
|
||
}
|
||
db.exec("UPDATE chat SET status = @status WHERE message_id = @message_id;",{
|
||
status = status;
|
||
message_id = _message_id;
|
||
} );
|
||
}
|
||
catch(e){
|
||
logger.error(e);
|
||
}
|
||
},mainForm, data, odm
|
||
)
|
||
}
|
||
//=========================使用单独的线程操作文件下载、上传、消息提醒=end=====================================
|
||
|
||
//==========================================监视消息============================================
|
||
mainForm.chatWatcherProc = function(){
|
||
thread.invoke(
|
||
function(mainForm, downAndUpFileThread){
|
||
import sqlite.latest;
|
||
import fsys;
|
||
import time;
|
||
import feishu;
|
||
import config;
|
||
import logger;
|
||
logger = logger.Logger();
|
||
|
||
var Transsioner = feishu.FeiShu(config.__feishu.appConfig.app_id, config.__feishu.appConfig.app_secret);
|
||
var db = sqlite("/sqlite.db"); //创建数据库
|
||
if( not db.existsTable("chat") ){
|
||
db.exec( "CREATE TABLE [chat](create_time, chat_id, message_id, msg_type, body, data, status);"); //创建表
|
||
}
|
||
|
||
//=========================循环读取群组历史消息,检查是否有用户上传文件========================================
|
||
while(true){
|
||
try{
|
||
//获取群组历史消息
|
||
var msg_count = 10;
|
||
var access_token = Transsioner.getAccessToken(config.__feishu.api.access_token_url);
|
||
var chat_history_msg = Transsioner.getChatHistoryMsg(config.__feishu.api.messages_url, config.__feishu.chat_id.a, msg_count, access_token);
|
||
//end
|
||
|
||
for(k, data in chat_history_msg){
|
||
var _sender_type = data["sender"]["sender_type"];
|
||
if(_sender_type != "user"){continue;}
|
||
|
||
var _create_time = data["create_time"];
|
||
var _chat_id = data["chat_id"];
|
||
var _message_id = data["message_id"];
|
||
var _msg_type = data["msg_type"];
|
||
var _body = data["body"];
|
||
var _data = data;
|
||
|
||
//查询该消息是否已记录
|
||
var result = db.stepQuery("SELECT * FROM [chat]",{ message_id = _message_id})
|
||
if(result != null){continue;}
|
||
|
||
//新消息加入数据库记录
|
||
var cmd = db.prepare("REPLACE INTO chat VALUES (@create_time, @chat_id, @message_id, @msg_type, @body, @data, @status);" )
|
||
cmd.step(
|
||
create_time = _create_time;
|
||
chat_id = _chat_id;
|
||
message_id = _message_id;
|
||
msg_type = _msg_type;
|
||
body = _body;
|
||
data = _data;
|
||
status = "";
|
||
)
|
||
|
||
//判断是否为文件消息
|
||
if(_msg_type == "file"){
|
||
var _sender_id = data["sender"]["id"];
|
||
var sender_user = null;
|
||
|
||
for(k,v in config.__feishu.user_id){
|
||
if( v == _sender_id){
|
||
sender_user = k;//获取发送该消息的用户,用于将文件放入对应路径,以及在对应群聊中发送提醒。
|
||
}
|
||
}
|
||
if(sender_user == null){
|
||
logger.error("未定义该用户发送的文件将拷贝到何处。");
|
||
continue;
|
||
}
|
||
|
||
logger.debug("检测到新文件,发送自用户【"++sender_user++"】:"++_sender_id);
|
||
|
||
var odm = null;
|
||
for(k,v in config.__user2ODM){
|
||
if( k == sender_user){
|
||
odm = v;
|
||
}
|
||
}
|
||
logger.debug("新文件即将上传至【"++odm++"】:"++config.__copy2Folder[odm]);
|
||
//用新线程
|
||
downAndUpFileThread(mainForm, data, odm);
|
||
//end
|
||
}
|
||
}
|
||
}
|
||
catch(e){
|
||
logger.error(e);
|
||
}
|
||
//检测频率
|
||
sleep(1000);
|
||
}
|
||
//=========================循环读取群组历史消息,检查是否有用户上传文件=end=====================================
|
||
},mainForm,downAndUpFileThread
|
||
)
|
||
}
|
||
//mainForm.chatWatcherProc();
|
||
|
||
//==========================================监视文件,有新增文件时上传至oss(todo)========================================
|
||
var fileWatcherProc = function(){
|
||
for(odm, dir in config.__watchFolder){
|
||
var watchDir = dir;
|
||
//创建监视线程
|
||
mainForm.thrdWatcher = fsys.dirWatcher.thread(
|
||
function(filename,action,actionText){
|
||
var fullname = watchDir + "\" + filename;
|
||
var newDirs = {};
|
||
select(action) {
|
||
case 1 {//添加文件
|
||
//mainForm.richedit.print(fullname);
|
||
if(fsys.isDir(fullname)){
|
||
|
||
table.push(newDirs, fullname);
|
||
}
|
||
//创建新的工作线程-用于处理耗时的事情,不阻塞下一个新文件的处理
|
||
thread.invoke(
|
||
function(mainForm){
|
||
sleep(5000)
|
||
},mainForm
|
||
)
|
||
}
|
||
case 2 {//移除文件
|
||
}
|
||
case 3 {//文件被修改
|
||
}
|
||
case 4 {//重命名:原文件名
|
||
}
|
||
case 5 {//重命名:新文件名
|
||
}
|
||
else {}
|
||
}
|
||
}, watchDir);
|
||
}
|
||
}
|
||
//fileWatcherProc();
|
||
|
||
|
||
//==========================================vlist============================================
|
||
/*
|
||
mainForm.vlist.setHeaderHeight(30);
|
||
mainForm.vlist.onlyText = true;
|
||
mainForm.vlist.ellipsion = true;
|
||
var t = { fields={"序号","时间","ODM","文件","状态"} };
|
||
*/
|
||
/*
|
||
for(i=1;10;1){
|
||
var tt={};
|
||
tt["序号"]="[@rowindex]"; // 行序号标记是不允许编辑修改的
|
||
tt["时间"]=time.now();
|
||
tt["ODM"]=string.random("易景","萨瑞","英迈");
|
||
tt["文件"]=math.random(1000,9999);
|
||
tt["状态"]=math.random(0,100);
|
||
..table.push(t,tt);
|
||
}
|
||
*/
|
||
//mainForm.vlist.setTable(t,,{60,150,60,-1,100},1);
|
||
|
||
//mainForm.vlist.setSortMark("▲" /*升序符号*/,"▼" /*降序符号*/,"" /*默认符号*/);
|
||
//mainForm.vlist.onSortColumn = function(col,desc){
|
||
// /*点击列标题进行排序。col:列号,从1开始。desc:是否倒序。*/
|
||
// owner.sort( col, desc, 0 /*数据转换:0默认 1时间 2数值 3文本*/ ,false /*使用微软api进行文本排序*/ );
|
||
//}
|
||
|
||
/**
|
||
mainForm.vlist.onDrawCellRect = function(row,col,hdc,rc,bkcolor,text,font,colalign){
|
||
/*绘制单元格有效区域背景(不含内容),返回ture则忽略原有效区域背景,否则继续绘制有效区域原背景*/
|
||
/*如果定义的drawCellRect()函数返回true,则本函数无效。*/
|
||
var num = tonumber(text);
|
||
if (col==5){
|
||
gdi.fillRect(hdc,0xEECCFF,rc);
|
||
var w = math.round(rc.width()*num/100);
|
||
for(i=0;w-1;5){
|
||
var left = rc.left+i;
|
||
var right = rc.left+i+4;
|
||
if right>left+w right=left+w;
|
||
var rect = ::RECT(left,rc.top,right,rc.bottom);
|
||
gdi.fillRect(hdc,0x55cc55,rect);
|
||
}
|
||
var f = ..table.clone(font)
|
||
f.color=0xFFFFFF
|
||
var rc1=table.clone(rc)
|
||
rc1.offset(-1,-1)
|
||
gdi.drawText(hdc,f,text++"%",rc1,colalign|4/*_DT_VCENTER*/|0x20/*_DT_SINGLELINE*/)
|
||
rc1.offset(2,2)
|
||
gdi.drawText(hdc,f,text++"%",rc1,colalign|4/*_DT_VCENTER*/|0x20/*_DT_SINGLELINE*/)
|
||
rc1.offset(-2,0)
|
||
gdi.drawText(hdc,f,text++"%",rc1,colalign|4/*_DT_VCENTER*/|0x20/*_DT_SINGLELINE*/)
|
||
rc1.offset(2,-2)
|
||
gdi.drawText(hdc,f,text++"%",rc1,colalign|4/*_DT_VCENTER*/|0x20/*_DT_SINGLELINE*/)
|
||
font.color = 0x000000
|
||
gdi.drawText(hdc,font,text++"%",rc,colalign|4/*_DT_VCENTER*/|0x20/*_DT_SINGLELINE*/)
|
||
return true;
|
||
}
|
||
}
|
||
**/
|
||
|
||
var main = function(){
|
||
mainForm.chatWatcherProc();
|
||
logger.info("已开始监测群聊文件消息");
|
||
}
|
||
|
||
main();
|
||
|
||
import fsys.file;
|
||
import thread.event;
|
||
mainForm.onClose = function(hwnd,message,wParam,lParam){
|
||
if(!mainForm.thrdWatcher) return;
|
||
mainForm.thrdWatcher.close(); //停止监视文件
|
||
}
|
||
|
||
mainForm.show(true);
|
||
win.loopMessage();
|
||
return mainForm;
|