n8n-nodes-onebot-api
Version:
OneBot node for n8n workflow, now you can send message to your QQ friend or group. This version includes optimized group and private chat operations and enhanced permission grading. OneBot 节点用于 n8n 工作流,现在您可以向您的 QQ 朋友或群组发送消息。此版本包括优化的群聊和私聊操作以及增强的权限分级。
398 lines • 16.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGroupMemberList = exports.getGroupList = exports.getFriendList = void 0;
const GenericFunctions_1 = require("./GenericFunctions");
async function getFriendList() {
try {
let currentInput = '';
try {
currentInput = this.getNodeParameter('user_id', '');
console.log('getFriendList - 当前输入的user_id值:', currentInput);
}
catch (error) {
console.log('无法获取当前user_id值:', error instanceof Error ? error.message : String(error));
}
const isValidQQNumber = currentInput && typeof currentInput === 'string' && /^\d+$/.test(currentInput);
const hasSearchInput = currentInput && typeof currentInput === 'string' && currentInput.trim() !== '';
const response = await GenericFunctions_1.apiRequest.call(this, 'GET', 'get_friend_list');
let friendData = [];
if (response && typeof response === 'object') {
if (response.data && Array.isArray(response.data)) {
friendData = response.data;
}
}
if (!friendData || friendData.length === 0) {
console.log('未找到好友数据');
if (isValidQQNumber) {
return [
{
name: `使用QQ号: ${currentInput}`,
value: currentInput,
description: '直接使用输入的QQ号',
},
];
}
return [{ name: '未找到好友', value: '', description: '好友列表为空或无法获取好友列表' }];
}
console.log(`成功获取到 ${friendData.length} 个好友`);
let friendOptions = friendData.map((info) => {
const userId = info.user_id;
const nickname = info.nickname || '未知昵称';
const remark = info.remark || '';
const displayName = remark || nickname;
return {
name: `${displayName} (QQ: ${userId})`,
value: userId,
description: `QQ: ${userId}`,
searchValues: {
displayName: displayName.toLowerCase(),
userId: userId.toString().toLowerCase(),
},
};
});
if (hasSearchInput) {
const searchValue = currentInput.toLowerCase();
const filteredOptions = friendOptions.filter((option) => {
return (option.searchValues.displayName.includes(searchValue) ||
option.searchValues.userId.includes(searchValue));
});
if (filteredOptions.length === 0 && isValidQQNumber) {
friendOptions = [
{
name: `使用QQ号: ${currentInput}`,
value: currentInput,
description: '直接使用输入的QQ号',
searchValues: {
displayName: '',
userId: currentInput,
},
},
];
}
else if (filteredOptions.length > 0) {
friendOptions = filteredOptions;
}
}
const finalOptions = friendOptions.map(({ name, value, description, }) => ({
name,
value,
description,
}));
console.log(`最终返回 ${finalOptions.length} 个有效的好友选项`);
return finalOptions;
}
catch (error) {
console.error('获取好友列表失败:', error instanceof Error ? error.message : String(error));
let currentInput = '';
try {
currentInput = this.getNodeParameter('user_id', '');
}
catch (error) {
}
if (currentInput && typeof currentInput === 'string' && /^\d+$/.test(currentInput)) {
return [
{
name: `使用QQ号: ${currentInput}`,
value: currentInput,
description: '直接使用输入的QQ号',
},
];
}
return [
{
name: '加载失败',
value: '',
description: '获取好友列表时出错',
},
];
}
}
exports.getFriendList = getFriendList;
async function getGroupList() {
try {
let currentInput = '';
try {
currentInput = this.getNodeParameter('group_id', '');
console.log('getGroupList - 当前输入的group_id值:', currentInput);
}
catch (error) {
console.log('无法获取当前group_id值:', error instanceof Error ? error.message : String(error));
}
const isValidGroupNumber = currentInput && typeof currentInput === 'string' && /^\d+$/.test(currentInput);
const hasSearchInput = currentInput && typeof currentInput === 'string' && currentInput.trim() !== '';
const response = await GenericFunctions_1.apiRequest.call(this, 'GET', 'get_group_list');
let groupData = [];
if (response && typeof response === 'object') {
if (response.data && Array.isArray(response.data)) {
groupData = response.data;
}
}
if (!groupData || groupData.length === 0) {
console.log('未找到群数据');
if (isValidGroupNumber) {
return [
{
name: `使用群号: ${currentInput}`,
value: currentInput,
description: '直接使用输入的群号',
},
];
}
return [{ name: '未找到群', value: '', description: '群列表为空或无法获取群列表' }];
}
console.log(`成功获取到 ${groupData.length} 个群`);
let groupOptions = groupData.map((info) => {
const groupId = info.group_id;
const groupName = info.group_name || '未知群名称';
return {
name: `${groupName} (群号: ${groupId})`,
value: groupId,
description: `群号: ${groupId}`,
searchValues: {
groupName: groupName.toLowerCase(),
groupId: groupId.toString().toLowerCase(),
},
};
});
if (hasSearchInput) {
const searchValue = currentInput.toLowerCase();
const filteredOptions = groupOptions.filter((option) => {
return (option.searchValues.groupName.includes(searchValue) ||
option.searchValues.groupId.includes(searchValue));
});
if (filteredOptions.length === 0 && isValidGroupNumber) {
groupOptions = [
{
name: `使用群号: ${currentInput}`,
value: currentInput,
description: '直接使用输入的群号',
searchValues: {
groupName: '',
groupId: currentInput,
},
},
];
}
else if (filteredOptions.length > 0) {
groupOptions = filteredOptions;
}
}
const finalOptions = groupOptions.map(({ name, value, description, }) => ({
name,
value,
description,
}));
console.log(`最终返回 ${finalOptions.length} 个有效的群选项`);
return finalOptions;
}
catch (error) {
console.error('获取群列表失败:', error instanceof Error ? error.message : String(error));
let currentInput = '';
try {
currentInput = this.getNodeParameter('group_id', '');
}
catch (error) {
}
if (currentInput && /^\d+$/.test(currentInput)) {
return [
{
name: `使用群号: ${currentInput}`,
value: currentInput,
description: '直接使用输入的群号',
},
];
}
return [
{
name: '加载失败',
value: '',
description: '获取群列表时出错',
},
];
}
}
exports.getGroupList = getGroupList;
async function getGroupMemberList() {
try {
let group_id;
try {
group_id = this.getNodeParameter('group_id');
console.log('获取到group_id:', group_id, '类型:', typeof group_id);
}
catch (error) {
console.log('获取group_id失败:', error instanceof Error ? error.message : String(error));
return [
{
name: '请先选择群组',
value: '',
description: '需要先在"Group Name or ID"字段中选择一个群组',
},
];
}
if (group_id === undefined || group_id === null || group_id === '') {
console.log('group_id无效或为空');
return [
{
name: '请先选择群组',
value: '',
description: '需要先在"Group Name or ID"字段中选择一个群组',
},
];
}
let currentInput = '';
try {
currentInput = this.getNodeParameter('user_id', '');
console.log('当前输入的user_id值:', currentInput);
}
catch (error) {
console.log('无法获取当前user_id值:', error instanceof Error ? error.message : String(error));
}
const isValidQQNumber = currentInput && typeof currentInput === 'string' && /^\d+$/.test(currentInput);
const hasSearchInput = currentInput && typeof currentInput === 'string' && currentInput.trim() !== '';
console.log('正在调用API获取群成员列表, 群ID:', group_id);
const query = { group_id };
const response = await GenericFunctions_1.apiRequest.call(this, 'GET', 'get_group_member_list', undefined, query);
console.log('API响应类型:', typeof response, '是否为数组:', Array.isArray(response));
let memberData = [];
if (response && typeof response === 'object') {
if (Array.isArray(response)) {
memberData = response;
console.log('API返回了数组格式的成员数据');
}
else if (response.data && Array.isArray(response.data)) {
memberData = response.data;
console.log('API返回了标准格式的成员数据');
}
else {
const possibleDataFields = ['result', 'members', 'list', 'member_list'];
for (const field of possibleDataFields) {
if (response[field] && Array.isArray(response[field])) {
memberData = response[field];
console.log(`从字段 ${field} 获取到成员数据`);
break;
}
}
}
}
if (!memberData || memberData.length === 0) {
console.log('未找到有效的群成员数据');
if (isValidQQNumber) {
return [
{
name: `使用QQ号: ${currentInput}`,
value: currentInput,
description: '直接使用输入的QQ号',
},
];
}
return [{ name: '未找到群成员', value: '', description: '该群没有成员或无法获取成员列表' }];
}
console.log(`成功获取到${memberData.length}个群成员`);
for (let i = 0; i < Math.min(memberData.length, 3); i++) {
console.log(`成员 ${i + 1} 数据:`, JSON.stringify(memberData[i]));
}
const validMembers = memberData.filter((info) => {
const userId = info.user_id || info.userId || info.uin || info.id;
return !!userId && userId !== '' && userId !== 0;
});
console.log(`有效成员数量: ${validMembers.length},过滤掉 ${memberData.length - validMembers.length} 个无效成员`);
const getRoleLabel = (role) => {
switch (role) {
case 'owner':
return '群主';
case 'admin':
return '管理员';
default:
return '成员';
}
};
let memberOptions = validMembers
.map((info) => {
const userId = info.user_id || info.userId || info.uin || info.id || '';
if (!userId)
return null;
let displayName = '';
if (info.card && info.card.trim() !== '') {
displayName = info.card;
}
else if (info.nickname && info.nickname.trim() !== '') {
displayName = info.nickname;
}
else if (info.name && info.name.trim() !== '') {
displayName = info.name;
}
else {
displayName = `成员${userId}`;
}
const role = info.role || info.permission || 'member';
const roleLabel = getRoleLabel(role);
return {
name: `${displayName}${role !== 'member' ? ` (${roleLabel})` : ''} (QQ: ${userId})`,
value: userId,
description: `QQ: ${userId}`,
searchValues: {
displayName: displayName.toLowerCase(),
userId: userId.toString().toLowerCase(),
},
};
})
.filter((item) => item !== null);
if (hasSearchInput) {
const searchValue = currentInput.toLowerCase();
const filteredOptions = memberOptions.filter((option) => {
return (option.searchValues.displayName.includes(searchValue) ||
option.searchValues.userId.includes(searchValue));
});
if (filteredOptions.length === 0 && isValidQQNumber) {
memberOptions = [
{
name: `使用QQ号: ${currentInput}`,
value: currentInput,
description: '直接使用输入的QQ号',
searchValues: {
displayName: '',
userId: currentInput,
},
},
];
}
else if (filteredOptions.length > 0) {
memberOptions = filteredOptions;
}
}
const finalOptions = memberOptions.map(({ name, value, description, }) => ({
name,
value,
description,
}));
console.log(`最终返回 ${finalOptions.length} 个有效的成员选项`);
return finalOptions;
}
catch (error) {
console.error('获取群成员列表失败:', error instanceof Error ? error.message : String(error));
let currentInput = '';
try {
currentInput = this.getNodeParameter('user_id', '');
}
catch (error) {
}
if (currentInput && typeof currentInput === 'string' && /^\d+$/.test(currentInput)) {
return [
{
name: `使用QQ号: ${currentInput}`,
value: currentInput,
description: '直接使用输入的QQ号',
},
];
}
return [
{
name: '加载失败',
value: '',
description: '获取群成员列表时出错,请确保选择了有效的群组且机器人有权限访问',
},
];
}
}
exports.getGroupMemberList = getGroupMemberList;
//# sourceMappingURL=SearchFunctions.js.map