添加获取必应Token及必应Token轻校验 (#157)

* feat: api3 WIP

* feat: add support for proxy of openai

* fix: if no proxy

* fix: optimize help message

* 增加必应token轻校验

经测试必应token固定长度215,但内容不确定

* feat: 绑定bing token后测试一下

---------

Co-authored-by: ikechan8370 <geyinchibuaa@gmail.com>
This commit is contained in:
Err0rCM 2023-02-16 17:45:57 +08:00 committed by GitHub
parent fffa8191d5
commit 6f60cd0ebc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,6 @@
import plugin from '../../../lib/plugins/plugin.js' import plugin from '../../../lib/plugins/plugin.js'
import {Config} from "../config/index.js";
import { BingAIClient } from '@waylaidwanderer/chatgpt-api'
export class ChatgptManagement extends plugin { export class ChatgptManagement extends plugin {
constructor (e) { constructor (e) {
@ -92,7 +94,29 @@ export class ChatgptManagement extends plugin {
async saveBingToken () { async saveBingToken () {
if (!this.e.msg) return if (!this.e.msg) return
let token = this.e.msg let token = this.e.msg
// todo 未知bing token是什么样的有号的可以加个校验在这 if (token.length < 215) {
await this.reply('Bing Token格式错误请确定获取了有效的_U Cookie或完整的Cookie', true)
this.finish('saveToken')
return
}
let cookie = undefined
if (token?.indexOf('=') > -1) {
cookie = token
}
const bingAIClient = new BingAIClient({
userToken: token, // "_U" cookie from bing.com
cookie,
debug: Config.debug
})
// 异步就好了不卡着这个context了
bingAIClient.createNewConversation().then(async res => {
if (res.clientId) {
logger.info("bing token 有效")
} else {
logger.error('bing token 无效', res)
await this.reply(`经检测Bing Token无效。来自Bing的错误提示${res.result?.message}`)
}
})
await redis.set('CHATGPT:BING_TOKEN', token) await redis.set('CHATGPT:BING_TOKEN', token)
await this.reply('Bing Token设置成功', true) await this.reply('Bing Token设置成功', true)
this.finish('saveBingToken') this.finish('saveBingToken')