mirror of
https://github.com/wahyd4/cloudflare-workers.git
synced 2026-08-08 21:05:20 +10:00
init
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
*.toml
|
||||||
|
**/node_modules
|
||||||
+90
@@ -0,0 +1,90 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
|
||||||
|
<!--[if IE 7]> <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
|
||||||
|
<!--[if IE 8]> <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
|
||||||
|
<!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
|
||||||
|
<head>
|
||||||
|
<title>Worker threw exception | home-tts.junv.workers.dev | Cloudflare</title></title>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||||
|
<meta name="robots" content="noindex, nofollow" />
|
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
||||||
|
<link rel="stylesheet" id="cf_styles-css" href="/cdn-cgi/styles/cf.errors.css" />
|
||||||
|
<!--[if lt IE 9]><link rel="stylesheet" id='cf_styles-ie-css' href="/cdn-cgi/styles/cf.errors.ie.css" /><![endif]-->
|
||||||
|
<style>body{margin:0;padding:0}</style>
|
||||||
|
|
||||||
|
|
||||||
|
<!--[if gte IE 10]><!-->
|
||||||
|
<script>
|
||||||
|
if (!navigator.cookieEnabled) {
|
||||||
|
window.addEventListener('DOMContentLoaded', function () {
|
||||||
|
var cookieEl = document.getElementById('cookie-alert');
|
||||||
|
cookieEl.style.display = 'block';
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<!--<![endif]-->
|
||||||
|
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="cf-wrapper">
|
||||||
|
<div class="cf-alert cf-alert-error cf-cookie-error" id="cookie-alert" data-translate="enable_cookies">Please enable cookies.</div>
|
||||||
|
<div id="cf-error-details" class="cf-error-details-wrapper">
|
||||||
|
<div class="cf-wrapper cf-header cf-error-overview">
|
||||||
|
<h1>
|
||||||
|
<span class="cf-error-type" data-translate="error">Error</span>
|
||||||
|
<span class="cf-error-code">1101</span>
|
||||||
|
<small class="heading-ray-id">Ray ID: 7a845ef3dc705ac4 • 2023-03-15 11:21:05 UTC</small>
|
||||||
|
</h1>
|
||||||
|
<h2 class="cf-subheadline" data-translate="error_desc">Worker threw exception</h2>
|
||||||
|
</div><!-- /.header -->
|
||||||
|
|
||||||
|
<section></section><!-- spacer -->
|
||||||
|
|
||||||
|
<div class="cf-section cf-wrapper">
|
||||||
|
<div class="cf-columns two">
|
||||||
|
<div class="cf-column">
|
||||||
|
<h2 data-translate="what_happened">What happened?</h2>
|
||||||
|
<p>You've requested a page on a website (home-tts.junv.workers.dev) that is on the <a href="https://www.cloudflare.com/5xx-error-landing/" target="_blank">Cloudflare</a> network. An unknown error occurred while rendering the page.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<div class="cf-column">
|
||||||
|
<h2 data-translate="what_can_i_do">What can I do?</h2>
|
||||||
|
<p><strong>If you are the owner of this website:</strong><br />you should <a href="https://www.cloudflare.com/login?utm_source=error_100x" target="_blank">login to Cloudflare</a> and check the error logs for home-tts.junv.workers.dev.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div><!-- /.section -->
|
||||||
|
|
||||||
|
<div class="cf-error-footer cf-wrapper w-240 lg:w-full py-10 sm:py-4 sm:px-8 mx-auto text-center sm:text-left border-solid border-0 border-t border-gray-300">
|
||||||
|
<p class="text-13">
|
||||||
|
<span class="cf-footer-item sm:block sm:mb-1">Cloudflare Ray ID: <strong class="font-semibold">7a845ef3dc705ac4</strong></span>
|
||||||
|
<span class="cf-footer-separator sm:hidden">•</span>
|
||||||
|
<span id="cf-footer-item-ip" class="cf-footer-item hidden sm:block sm:mb-1">
|
||||||
|
Your IP:
|
||||||
|
<button type="button" id="cf-footer-ip-reveal" class="cf-footer-ip-reveal-btn">Click to reveal</button>
|
||||||
|
<span class="hidden" id="cf-footer-ip">101.116.14.62</span>
|
||||||
|
<span class="cf-footer-separator sm:hidden">•</span>
|
||||||
|
</span>
|
||||||
|
<span class="cf-footer-item sm:block sm:mb-1"><span>Performance & security by</span> <a rel="noopener noreferrer" href="https://www.cloudflare.com/5xx-error-landing" id="brand_link" target="_blank">Cloudflare</a></span>
|
||||||
|
|
||||||
|
</p>
|
||||||
|
<script>(function(){function d(){var b=a.getElementById("cf-footer-item-ip"),c=a.getElementById("cf-footer-ip-reveal");b&&"classList"in b&&(b.classList.remove("hidden"),c.addEventListener("click",function(){c.classList.add("hidden");a.getElementById("cf-footer-ip").classList.remove("hidden")}))}var a=document;document.addEventListener&&a.addEventListener("DOMContentLoaded",d)})();</script>
|
||||||
|
</div><!-- /.error-footer -->
|
||||||
|
|
||||||
|
|
||||||
|
</div><!-- /#cf-error-details -->
|
||||||
|
</div><!-- /#cf-wrapper -->
|
||||||
|
|
||||||
|
<script>
|
||||||
|
window._cf_translation = {};
|
||||||
|
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
Generated
+2249
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "tts",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"devDependencies": {
|
||||||
|
"wrangler": "^2.12.3"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"itty-router": "^3.0.11",
|
||||||
|
"microsoft-cognitiveservices-speech-sdk": "^1.26.0",
|
||||||
|
"node-fetch": "^3.3.1",
|
||||||
|
"telegraf": "^4.12.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
+127
@@ -0,0 +1,127 @@
|
|||||||
|
|
||||||
|
import { Router } from 'itty-router'
|
||||||
|
|
||||||
|
const router = Router()
|
||||||
|
const botToken = BOT_TOKEN;
|
||||||
|
const chatId = CHAT_ID;
|
||||||
|
const ttsKey = TTS_KEY
|
||||||
|
const format = "audio-16khz-128kbitrate-mono-mp3";
|
||||||
|
// const defaultVoice = 'zh-CN-XiaoxiaoNeural'; //female
|
||||||
|
const defaultVoice = 'zh-CN-YunfengNeural'
|
||||||
|
// male
|
||||||
|
// zh-CN-YunfengNeural
|
||||||
|
|
||||||
|
async function sendToAzure(text: string, voice: string, format: string) {
|
||||||
|
const endpoint = `https://australiaeast.tts.speech.microsoft.com/cognitiveservices/v1`
|
||||||
|
const body = `<speak version="1.0" xml:lang="${voice}"><voice xml:lang="${voice}" xml:gender="Female" name="${voice}">${text}</voice></speak>`
|
||||||
|
|
||||||
|
const response = await fetch(`${endpoint}`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Ocp-Apim-Subscription-Key': ttsKey,
|
||||||
|
'Content-Type': 'application/ssml+xml',
|
||||||
|
'X-Microsoft-OutputFormat': format,
|
||||||
|
'User-Agent': 'curl'
|
||||||
|
},
|
||||||
|
body,
|
||||||
|
})
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to generate speech: ${response.statusText}`)
|
||||||
|
}
|
||||||
|
return response.arrayBuffer()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendVoice(chatId: number | string, voice: ArrayBuffer, caption?: string): Promise<boolean> {
|
||||||
|
const apiUrl = `https://api.telegram.org/bot${botToken}/sendVoice`;
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('chat_id', chatId.toString());
|
||||||
|
formData.append('voice', new Blob([voice], { type: 'audio/mpeg' }), 'voice.mp3');
|
||||||
|
if (caption) {
|
||||||
|
formData.append('caption', caption);
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(apiUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
|
||||||
|
const responseBody = await response.json();
|
||||||
|
if (responseBody.ok) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
console.error(`Telegram API returned error ${responseBody.error_code}: ${responseBody.description}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define the request handler
|
||||||
|
async function handleBotRequest(request) {
|
||||||
|
// Parse the request body as JSON
|
||||||
|
const requestBody = await request.json();
|
||||||
|
if (!requestBody.message) {
|
||||||
|
return new Response('Error: No valid message founded', { status: 400 });
|
||||||
|
}
|
||||||
|
const text = requestBody.message.text;
|
||||||
|
var audioData;
|
||||||
|
try {
|
||||||
|
// Generate the audio file from the input text
|
||||||
|
audioData = await sendToAzure(text, defaultVoice, format);
|
||||||
|
} catch (err) {
|
||||||
|
await new Response('Error: something went wrong', { status: 500 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await sendVoice(chatId, audioData)) {
|
||||||
|
return new Response(audioData, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'audio/mpeg',
|
||||||
|
'Content-Disposition': 'attachment; filename="audio.mp3"',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return new Response('Error: something went wrong', { status: 500 });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleRequest(request: Request): Promise<Response> {
|
||||||
|
try {
|
||||||
|
const params = request.query;
|
||||||
|
if (!params) {
|
||||||
|
return new Response('Error: No valid params provided', { status: 400 });
|
||||||
|
}
|
||||||
|
var voice = params.voice
|
||||||
|
const haha = params.haha
|
||||||
|
const text = params.text
|
||||||
|
|
||||||
|
if (haha != "hahaJunv") {
|
||||||
|
return new Response('Error: something went wrong, haha', { status: 500 });
|
||||||
|
}
|
||||||
|
if (voice == null) {
|
||||||
|
voice = defaultVoice
|
||||||
|
}
|
||||||
|
|
||||||
|
const audioData = await sendToAzure(text, voice, format)
|
||||||
|
|
||||||
|
return new Response(audioData, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'audio/mpeg',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
return new Response(error.message || 'Internal Server Error', {
|
||||||
|
status: 500,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define the Cloudflare Worker entry point
|
||||||
|
// addEventListener('fetch', event => {
|
||||||
|
// event.respondWith(handleRequest(event.request));
|
||||||
|
// });
|
||||||
|
|
||||||
|
router.post('/bot', handleBotRequest)
|
||||||
|
router.get('/tts', handleRequest)
|
||||||
|
|
||||||
|
addEventListener('fetch', event => {
|
||||||
|
event.respondWith(router.handle(event.request))
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user