Skip to content

Commit

Permalink
add voice
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Oct 9, 2024
1 parent 4c837b8 commit a0d7bd0
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/botInstance/coze.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getPuppetEol, isWindowsPlatform } from '../const/puppet-type.js'
import dayjs from 'dayjs'
import { extractImageLinks } from '../lib/index.js'
import nodeFetch from 'node-fetch'
import {getText2Speech} from "../proxy/multimodal.js";


class CozeAi {
Expand Down Expand Up @@ -127,6 +128,12 @@ class CozeAi {
parentMessageId: id
}
let replys = []
if(this.config?.openTTS) {
replys = await getText2Speech(text, this.config.ttsConfig)
if(replys.length) {
return replys
}
}
let message
if (this.config.showQuestion) {
message = `${content}${this.eol}-----------${this.eol}` + (this.iswindows ? text.replaceAll('\n', this.eol) : text)
Expand Down
7 changes: 7 additions & 0 deletions src/botInstance/cozev3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ContentCensor } from "../lib/contentCensor.js";
import { getPuppetEol, isWindowsPlatform } from '../const/puppet-type.js'
import dayjs from "dayjs";
import { extractImageLinks } from '../lib/index.js'
import {getText2Speech} from "../proxy/multimodal.js";


class CozeV3Ai {
Expand Down Expand Up @@ -101,6 +102,12 @@ class CozeV3Ai {
};
}
let replys = []
if(this.config?.openTTS) {
replys = await getText2Speech(text, this.config.ttsConfig)
if(replys.length) {
return replys
}
}
let message;
if(this.config.showQuestion) {
message = `${content}${this.eol}-----------${this.eol}` + (this.iswindows ? text.replaceAll('\n', this.eol) : text);
Expand Down
8 changes: 8 additions & 0 deletions src/botInstance/dify.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ContentCensor } from "../lib/contentCensor.js";
import { getPuppetEol, isWindowsPlatform } from '../const/puppet-type.js'
import dayjs from "dayjs";
import { extractImageLinks } from '../lib/index.js'
import {getText2Speech} from "../proxy/multimodal.js";


class DifyAi {
Expand Down Expand Up @@ -99,6 +100,13 @@ class DifyAi {
};
}
let replys = []
console.log('是否开启语音', this.config?.openTTS)
if(this.config?.openTTS) {
replys = await getText2Speech(text, this.config.ttsConfig)
if(replys.length) {
return replys
}
}
let message;
if(this.config.showQuestion) {
message = `${content}${this.eol}-----------${this.eol}` + (this.iswindows ? text.replaceAll('\n', this.eol) : text);
Expand Down
7 changes: 7 additions & 0 deletions src/botInstance/officialOpenAi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getPuppetEol, isWindowsPlatform } from '../const/puppet-type.js'
import { v4 as uuidv4 } from "uuid";
import dayjs from "dayjs";
import { extractImageLinks } from '../lib/index.js'
import {getText2Speech} from "../proxy/multimodal.js";
let chatGPT = null


Expand Down Expand Up @@ -198,6 +199,12 @@ class OfficialOpenAi {
}

let replys = []
if(this.config?.openTTS) {
replys = await getText2Speech(text, this.config.ttsConfig)
if(replys.length) {
return replys
}
}
let message;
if(this.config.showQuestion) {
message = `${content}${this.eol}-----------${this.eol}` + (this.iswindows ? text.replaceAll('\n', this.eol) : text);
Expand Down
7 changes: 7 additions & 0 deletions src/botInstance/qany.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ContentCensor } from '../lib/contentCensor.js'
import { getPuppetEol, isWindowsPlatform } from '../const/puppet-type.js'
import dayjs from 'dayjs'
import { extractImageLinks } from '../lib/index.js'
import {getText2Speech} from "../proxy/multimodal.js";

class QAnyAi {
constructor(config = {
Expand Down Expand Up @@ -102,6 +103,12 @@ class QAnyAi {
needHistory: true
}
let replys = []
if(this.config?.openTTS) {
replys = await getText2Speech(text, this.config.ttsConfig)
if(replys.length) {
return replys
}
}
let message
if (this.config.showQuestion) {
message = `${content}${this.eol}-----------${this.eol}` + (this.iswindows ? text.replaceAll('\n', this.eol) : text)
Expand Down
12 changes: 12 additions & 0 deletions src/handlers/on-scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ export function resetScanTime () {
scanTime = 0
}

function extractLastPathWithCheck(url) {
const parts = url.split('/');
if (parts[parts.length - 2] === 'x') {
return parts[parts.length - 1];
} else {
return null; // 或者可以返回一个错误消息
}
}

function getQrcodeKey(qrcode) {
if(!qrcode || !qrcode.startsWith('http')) return
let url = new URL(qrcode);
let searchParams = new URLSearchParams(url.search.slice(1));
if(searchParams.get('key')) {
console.log('获取到二维码信息中的key')
globalConfig.setQrKey(searchParams.get('key'))
} else if(extractLastPathWithCheck(qrcode)) {
console.log('获取到二维码信息中的key');
globalConfig.setQrKey(extractLastPathWithCheck(qrcode));
} else {
globalConfig.setQrKey('')
}
Expand Down
3 changes: 2 additions & 1 deletion src/proxy/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const AIBOTK = 'https://api-bot.aibotk.com/open/v1/api'
// export const AIBOTK = 'https://api-bot.aibotk.com/open/v1/api'
export const AIBOTK = 'http://127.0.0.1:7002/open/v1/api'
export const AIBOTK_OUTAPI = 'https://outapi.aibotk.com/open/api/v1'
export const TULING = 'http://openapi.tuling123.com/openapi/api/v2'
export const TXHOST = 'http://api.tianapi.com'
Expand Down
42 changes: 40 additions & 2 deletions src/proxy/multimodal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function getVoiceText(file, aiConfig) {
let config = {
method: 'post',
maxBodyLength: Infinity,
timeout: 30000,
timeout: 60000,
url: AIBOTK_OUTAPI + '/voice/text',
headers: {
...formData.getHeaders(),
Expand All @@ -43,6 +43,44 @@ export async function getVoiceText(file, aiConfig) {
}
}

/**
* 文字转语音
* @param text
* @param aiConfig
* @returns {Promise<*|string>}
*/
export async function getText2Speech(text, aiConfig) {
try {
const env = await getAibotConfig()
const { apiKey } = env
let config = {
method: 'post',
maxBodyLength: Infinity,
timeout: 60000,
url: AIBOTK_OUTAPI + '/text/speech',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
data : {
text,
aiConfig
}
};

const result = await axios.request(config)
if(result.data.code === 200) {
return result.data.data
} else {
console.log('语音生成出错', result.data.message)
return '';
}
} catch (e) {
console.log(`语音生成出错: ${e}`)
return '';
}
}

/**
* 识别图像
* @param images
Expand All @@ -57,7 +95,7 @@ export async function getImageVision(images, question, config) {
const reqConfig = {
method: 'post',
maxBodyLength: Infinity,
timeout: 30000,
timeout: 60000,
url: AIBOTK_OUTAPI + '/image/vision',
headers: {
Authorization: `Bearer ${apiKey}`,
Expand Down

0 comments on commit a0d7bd0

Please sign in to comment.