Skip to content

Commit

Permalink
Modify: Audio message to text.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimbly8836 committed Nov 26, 2024
1 parent d07a4c6 commit bac9e7e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"main": "app.ts",
"esModuleInterop": true,
"scripts": {
"dev": "ts-node src/app.ts NODE_ENV=development",
"dev": " NODE_ENV=development ts-node src/app.ts",
"build": "tsc",
"start": "tsc && node dist/app.js NODE_ENV=production"
"start": "NODE_ENV=production tsc && node dist/app.js"
},
"keywords": [],
"author": "",
Expand Down
15 changes: 10 additions & 5 deletions src/client/WechatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1251,20 +1251,25 @@ export class WeChatClient extends BaseClient {
create_time: new Date().getTime(),
})
if (this._tgClient.setting.getVariable(VariableType.SETTING_AUTO_TRANSCRIPT)) {
this.logInfo('audio file to buffer success')
SpeechService.getInstance().getTranscript(buffer).then(audioTranscript => {
sender.editAudio(tgMessage.chatId, res.message_id, `${identityStr}${audioTranscript}`)
}).catch(() => {
sender.editAudio(tgMessage.chatId, res.message_id, `${identityStr}${this.t('wechat.audioTranscriptFailed')}, ${this.t('wechat.plzViewOnPhone')}`)
this.logDebug('audioTranscript text: ', audioTranscript)
sender.editAudio(tgMessage.chatId, res.message_id, `${identityStr}\n${audioTranscript}`)
}).catch((reason) => {
this.logInfo('audioTranscript error: ', reason)
sender.editAudio(tgMessage.chatId, res.message_id, `${identityStr}\n${this.t('wechat.audioTranscriptFailed')}, ${this.t('wechat.plzViewOnPhone')}`)
})
}
}).catch(() => {
}).catch((reason) => {
this.logInfo('send file error:', reason)
this.sendMessageToTg({
...tgMessage,
body: `${this.t('wechat.get')}[${this.getMessageName(message.type())}]${this.t('common.error')}, ${this.t('wechat.plzViewOnPhone')}`
})
})
})
}).catch(() => {
}).catch((reason) => {
this.logInfo('file to buffer error:', reason)
this.sendMessageToTg({
...tgMessage,
body: `${this.t('wechat.get')}[${this.getMessageName(message.type())}]${this.t('common.error')}, ${this.t('wechat.plzViewOnPhone')}`
Expand Down
3 changes: 2 additions & 1 deletion src/message/TelegramApiMessageSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class TelegramApiMessageSender extends MessageSender {
const inputPeerChannelFromMessage = await this.sender.getInputEntity(chatId) || chatId
const sendParam: messageMethods.EditMessageParams = {
message: parseInt(msgId + ''),
text: caption
text: caption,
parseMode: 'html'
}
return new Promise((resolve, reject) => {
this.sender.editMessage(inputPeerChannelFromMessage, sendParam).then(res => {
Expand Down
3 changes: 2 additions & 1 deletion src/message/TelegramBotApiMessageSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export class TelegramBotApiMessageSender extends MessageSender {
editAudio(chatId: string | number, msgId: string | number, caption: string): Promise<SendResult> {
this.sendAction(Number(chatId), 'typing')
return new Promise<SendResult>((resolve, reject) => {
this.sender.telegram.editMessageCaption(chatId, parseInt(msgId + ''), undefined, caption, {}).then(() => {
this.sender.telegram.editMessageCaption(chatId, parseInt(msgId + ''), undefined, caption, {parse_mode: 'HTML'})
.then(() => {
resolve({message_id: msgId})
}).catch(e => reject(e))
})
Expand Down
23 changes: 9 additions & 14 deletions src/model/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as fs from 'node:fs'
import log4js from 'log4js'
import {LogUtils} from '../util/LogUtils'

export class VariableContainer {

Expand Down Expand Up @@ -62,23 +64,16 @@ export class VariableContainer {
// 将内容写入文件
writeToFile(filePath = `${StorageSettings.STORAGE_FOLDER}/${StorageSettings.SETTING_FILE_NAME}`): void {
try {
const data = {
[VariableType.SETTING_NOTION_MODE]: this.variables[VariableType.SETTING_NOTION_MODE] ? this.variables[VariableType.SETTING_NOTION_MODE] : NotionMode.BLACK,
[VariableType.SETTING_WHITE_LIST]: this.variables[VariableType.SETTING_WHITE_LIST] ? this.variables[VariableType.SETTING_WHITE_LIST] : [],
[VariableType.SETTING_BLACK_LIST]: this.variables[VariableType.SETTING_BLACK_LIST] ? this.variables[VariableType.SETTING_BLACK_LIST] : [],
[VariableType.SETTING_REPLY_SUCCESS]: this.variables[VariableType.SETTING_REPLY_SUCCESS] ? this.variables[VariableType.SETTING_REPLY_SUCCESS] : false,
[VariableType.SETTING_AUTO_SWITCH]: this.variables[VariableType.SETTING_AUTO_SWITCH] ? this.variables[VariableType.SETTING_AUTO_SWITCH] : false,
[VariableType.SETTING_FORWARD_SELF]: this.variables[VariableType.SETTING_FORWARD_SELF] ? this.variables[VariableType.SETTING_FORWARD_SELF] : false,
[VariableType.SETTING_COMPRESSION]: this.variables[VariableType.SETTING_COMPRESSION] ? this.variables[VariableType.SETTING_COMPRESSION] : false,
[VariableType.SETTING_ACCEPT_OFFICIAL_ACCOUNT]: this.variables[VariableType.SETTING_ACCEPT_OFFICIAL_ACCOUNT] ? this.variables[VariableType.SETTING_ACCEPT_OFFICIAL_ACCOUNT] : false,
[VariableType.SETTING_BLOCK_EMOTICON]: this.variables[VariableType.SETTING_BLOCK_EMOTICON] ? this.variables[VariableType.SETTING_BLOCK_EMOTICON] : false,
[VariableType.SETTING_AUTO_GROUP]: this.variables[VariableType.SETTING_AUTO_GROUP] ? this.variables[VariableType.SETTING_AUTO_GROUP] : false,
[VariableType.SETTING_LANGUAGE]: this.variables[VariableType.SETTING_LANGUAGE] ? this.variables[VariableType.SETTING_LANGUAGE] : 'zh',
const data: { [key: string]: any } = {}
for (const key in this.variables) {
if (Object.prototype.hasOwnProperty.call(this.variables, key)) {
data[key] = this.variables[key]
}
}
fs.writeFileSync(filePath, JSON.stringify(data), 'utf8')
console.log('File written successfully.')
LogUtils.debugLog().debug('Write to file:', data)
} catch (error) {
console.error('Error writing to file:', error)
LogUtils.errorLog().error('Write to file error:', error)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/service/SpeechService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export class SpeechService {
secretId: process.env.TENCENT_SECRET_ID,
secretKey: process.env.TENCENT_SECRET_KEY,
},
region: "ap-guangzhou",
region: 'ap-guangzhou',
profile: {
signMethod: "TC3-HMAC-SHA256",
signMethod: 'TC3-HMAC-SHA256',
httpProfile: {
reqMethod: "POST",
reqMethod: 'POST',
reqTimeout: 30,
},
},
Expand All @@ -33,13 +33,13 @@ export class SpeechService {

public async getTranscript(audioBuffer: Buffer): Promise<string> {
const base64Audio = audioBuffer.toString('base64')

const params: SentenceRecognitionRequest = {
EngSerViceType: "16k_zh",
EngSerViceType: '16k_zh',
SourceType: 1,
Data: base64Audio,
DataLen: audioBuffer.length,
VoiceFormat: "mp3",
VoiceFormat: 'mp3',
}

const result = await this.client.SentenceRecognition(params)
Expand Down
14 changes: 13 additions & 1 deletion src/util/LogUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import log4js from 'log4js'
import log4js, {Logger} from 'log4js'

export class LogUtils {

Expand Down Expand Up @@ -26,4 +26,16 @@ export class LogUtils {
}
})
}

public static errorLog(): Logger {
return LogUtils.config().getLogger('error')
}

public static infoLog(): Logger {
return LogUtils.config().getLogger('info')
}

public static debugLog(): Logger {
return LogUtils.config().getLogger('debug')
}
}

0 comments on commit bac9e7e

Please sign in to comment.