forked from TBXark/ChatGPT-Telegram-Workers
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: optimize logic of the /set command.
feat: add judgment for whitelist users in inlineQuery. chore: optimize the prompt message after tg message send failed.
- Loading branch information
Showing
11 changed files
with
118 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { ReadableStream as WebReadableStream } from 'node:stream/web'; | ||
import { Readable, Transform } from 'node:stream'; | ||
|
||
class Base64Encode extends Transform { | ||
private bufferCache = ''; | ||
|
||
_transform(chunk: Buffer, encoding: string, callback: () => void) { | ||
this.bufferCache += chunk.toString('base64'); | ||
callback(); | ||
} | ||
|
||
_flush(callback: () => void) { | ||
if (this.bufferCache.length) { | ||
this.push(this.bufferCache); | ||
} | ||
callback(); | ||
} | ||
} | ||
|
||
async function streamToBase64(audioUrl: string) { | ||
try { | ||
const response = await fetch(audioUrl); | ||
if (!response.ok) { | ||
throw new Error(`Failed to download file: ${response.statusText}`); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const base64EncodeStream = new Base64Encode(); | ||
let base64Data = ''; | ||
|
||
base64EncodeStream.on('data', chunk => base64Data += chunk); | ||
base64EncodeStream.on('end', () => resolve(base64Data)); | ||
base64EncodeStream.on('error', reject); | ||
|
||
// 将 Web Stream 转换为 Node Stream | ||
Readable.fromWeb(response.body as unknown as WebReadableStream).pipe(base64EncodeStream); | ||
}); | ||
} catch (error) { | ||
console.error('Error processing stream:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
export { streamToBase64 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters