From 5ccefa6fa674e507246a7ac792329c559162cf30 Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Sat, 24 Jun 2023 17:53:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=90=9C=E5=9B=BE=E5=92=8C=E5=8F=91?= =?UTF-8?q?=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/chat.js | 4 ++++ utils/tools/SearchImageTool.js | 30 +++++++++++++++++++++++ utils/tools/SendPictureTool.js | 44 ++++++++++++++++++++-------------- 3 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 utils/tools/SearchImageTool.js diff --git a/apps/chat.js b/apps/chat.js index 5aa9903..fa316e6 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -58,6 +58,8 @@ import { WebsiteTool } from '../utils/tools/WebsiteTool.js' import { WeatherTool } from '../utils/tools/WeatherTool.js' import { SerpTool } from '../utils/tools/SerpTool.js' import { SerpIkechan8370Tool } from '../utils/tools/SerpIkechan8370Tool.js' +import {SendPictureTool} from "../utils/tools/SendPictureTool.js"; +import {SerpImageTool} from "../utils/tools/SearchImageTool.js"; try { await import('emoji-strip') } catch (err) { @@ -1963,6 +1965,8 @@ export class chatgpt extends plugin { new JinyanTool(), new KickOutTool(), new WeatherTool(), + new SendPictureTool(), + new SerpImageTool(), serpTool ] // if (e.sender.role === 'admin' || e.sender.role === 'owner') { diff --git a/utils/tools/SearchImageTool.js b/utils/tools/SearchImageTool.js new file mode 100644 index 0000000..4d53e95 --- /dev/null +++ b/utils/tools/SearchImageTool.js @@ -0,0 +1,30 @@ +import { AbstractTool } from './AbstractTool.js' + +export class SerpImageTool extends AbstractTool { + name = 'searchImage' + + parameters = { + properties: { + q: { + type: 'string', + description: 'search keyword' + } + }, + required: ['q'] + } + + func = async function (opts) { + let { q } = opts + let serpRes = await fetch(`https://serp.ikechan8370.com/image/bing?q=${encodeURIComponent(q)}`, { + headers: { + 'X-From-Library': 'ikechan8370' + } + }) + serpRes = await serpRes.json() + + let res = serpRes.data + return `the images search results are here in json format:\n${JSON.stringify(res)}. the murl field is real picture url. You should use sendPicture to send them` + } + + description = 'Useful when you want to search images from the internet. ' +} diff --git a/utils/tools/SendPictureTool.js b/utils/tools/SendPictureTool.js index 6ffc003..ef08571 100644 --- a/utils/tools/SendPictureTool.js +++ b/utils/tools/SendPictureTool.js @@ -1,33 +1,41 @@ -import {AbstractTool} from "./AbstractTool.js"; - +import { AbstractTool } from './AbstractTool.js' export class SendPictureTool extends AbstractTool { name = 'sendPicture' parameters = { - picture: { - type: 'string', - description: '图片的url,多个用空格隔开' - }, - groupId: { - type: 'string', - description: '群号或qq号,发送目标' + properties: { + picture: { + type: 'string', + description: 'the url of the pictures, split with space if more than one ' + }, + groupId: { + type: 'string', + description: '群号或qq号,发送目标' + } }, required: ['picture', 'groupId'] } - func = async function (picture, groupId) { + func = async function (opt) { + let { picture, groupId } = opt let pictures = picture.trim().split(' ') pictures = pictures.map(img => segment.image(img)) let groupList = await Bot.getGroupList() - if (groupList.get(groupId)) { - let group = await Bot.pickGroup(groupId) - await group.sendMsg(pictures) - } else { - let user = await Bot.pickFriend(groupId) - await user.sendMsg(pictures) + try { + if (groupList.get(groupId)) { + let group = await Bot.pickGroup(groupId) + await group.sendMsg(pictures) + return `picture has been sent to group ${groupId}` + } else { + let user = await Bot.pickFriend(groupId) + await user.sendMsg(pictures) + return `picture has been sent to user ${groupId}` + } + } catch (err) { + return `failed to send pictures, error: ${err.toString()}` } } - description = 'Useful when you want to send some pictures. The input to this tool should be the url of the pictures and the group number or the user\'s qq number, each url and the group number or qq number should be concated with a space, and the group number or qq number should be the last. 如果是在群聊中,优先选择群号发送。' -} \ No newline at end of file + description = 'Useful when you want to send one or more pictures. ' +}