Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
katsuhira02 committed Jun 7, 2024
1 parent 11e456f commit 2f2c699
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 200 deletions.
186 changes: 93 additions & 93 deletions supabase/functions/_shared/supabase/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
model_ai,
SITE_URL,
SYNC_LABS_API_KEY,
XI_API_KEY,
// XI_API_KEY,
} from "../constants.ts";
import { openai } from "../openai/client.ts";
import GPT3Tokenizer from "https://esm.sh/[email protected]";
Expand Down Expand Up @@ -236,7 +236,7 @@ export async function createVoiceSyncLabs(
const response = await fetch(url, {
method: "POST",
headers: {
"xi-api-key": SYNC_LABS_API_KEY as string,
"x-api-key": SYNC_LABS_API_KEY as string,
"Content-Type": "application/json",
},
body,
Expand All @@ -256,106 +256,106 @@ export async function createVoiceSyncLabs(
}
}

export const deleteVoice = async (voiceId: string) => {
const XI_API_KEY = Deno.env.get("XI_API_KEY");
// export const deleteVoice = async (voiceId: string) => {
// const XI_API_KEY = Deno.env.get("XI_API_KEY");

const response = await fetch(
`https://api.elevenlabs.io/v1/voices/${voiceId}`,
{
method: "DELETE",
headers: {
"xi-api-key": XI_API_KEY as string,
},
},
);

if (!response.ok) {
throw new Error(`Failed to delete voice: ${response.statusText}`);
}

return await response.json();
};
// const response = await fetch(
// `https://api.elevenlabs.io/v1/voices/${voiceId}`,
// {
// method: "DELETE",
// headers: {
// "xi-api-key": XI_API_KEY as string,
// },
// },
// );

export const getVoiceId = async (telegramId: string) => {
const { data, error } = await supabase
.from("users")
.select("voice_id_elevenlabs")
.eq("telegram_id", telegramId)
.single();
// if (!response.ok) {
// throw new Error(`Failed to delete voice: ${response.statusText}`);
// }

if (error) {
throw new Error(`Failed to fetch voice_id_elevenlabs: ${error.message}`);
}
// return await response.json();
// };

return data.voice_id_elevenlabs;
};
// export const getVoiceId = async (telegramId: string) => {
// const { data, error } = await supabase
// .from("users")
// .select("voice_id_elevenlabs")
// .eq("telegram_id", telegramId)
// .single();

export const createVoiceMessage = async (
text: string,
voiceId: string,
botToken: string,
chatId: string,
) => {
console.log(text, "text createVoiceMessage");
console.log(voiceId, "voiceId createVoiceMessage");

const url = `https://api.elevenlabs.io/v1/text-to-speech/${voiceId}`;
const options = {
method: "POST",
headers: {
'Content-Type': 'application/json',
'xi-api-key': XI_API_KEY as string,
},
body: JSON.stringify({
text,
voice_settings: {
stability: 0.5,
similarity_boost: 0.5,
},
}),
};

try {
const response = await fetch(url, options);
console.log("response createVoiceMessage", response);
// if (error) {
// throw new Error(`Failed to fetch voice_id_elevenlabs: ${error.message}`);
// }

if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to fetch audio: ${response.statusText} - ${errorText}`);
}
// return data.voice_id_elevenlabs;
// };

// export const createVoiceMessage = async (
// text: string,
// voiceId: string,
// botToken: string,
// chatId: string,
// ) => {
// console.log(text, "text createVoiceMessage");
// console.log(voiceId, "voiceId createVoiceMessage");

// const url = `https://api.elevenlabs.io/v1/text-to-speech/${voiceId}`;
// const options = {
// method: "POST",
// headers: {
// 'Content-Type': 'application/json',
// 'xi-api-key': XI_API_KEY as string,
// },
// body: JSON.stringify({
// text,
// voice_settings: {
// stability: 0.5,
// similarity_boost: 0.5,
// },
// }),
// };

const arrayBuffer = await response.arrayBuffer();
const buffer = new Uint8Array(arrayBuffer);
// try {
// const response = await fetch(url, options);
// console.log("response createVoiceMessage", response);

const formData = new FormData();
formData.append("chat_id", chatId);
formData.append(
"audio",
new Blob([buffer], { type: "audio/mpeg" }),
"voice_message.mp3",
);
// if (!response.ok) {
// const errorText = await response.text();
// throw new Error(`Failed to fetch audio: ${response.statusText} - ${errorText}`);
// }

const telegramResponse = await fetch(
`https://api.telegram.org/bot${botToken}/sendAudio`,
{
method: "POST",
body: formData,
},
);
// const arrayBuffer = await response.arrayBuffer();
// const buffer = new Uint8Array(arrayBuffer);

// const formData = new FormData();
// formData.append("chat_id", chatId);
// formData.append(
// "audio",
// new Blob([buffer], { type: "audio/mpeg" }),
// "voice_message.mp3",
// );

// const telegramResponse = await fetch(
// `https://api.telegram.org/bot${botToken}/sendAudio`,
// {
// method: "POST",
// body: formData,
// },
// );

if (!telegramResponse.ok) {
const errorText = await telegramResponse.text();
throw new Error(
`Failed to send audio message: ${telegramResponse.statusText} - ${errorText}`,
);
}
// if (!telegramResponse.ok) {
// const errorText = await telegramResponse.text();
// throw new Error(
// `Failed to send audio message: ${telegramResponse.statusText} - ${errorText}`,
// );
// }

const telegramData = await telegramResponse.json();
console.log("telegramData", telegramData);
// const telegramData = await telegramResponse.json();
// console.log("telegramData", telegramData);

return telegramData;
} catch (err) {
console.error(err);
throw err;
}
};
// return telegramData;
// } catch (err) {
// console.error(err);
// throw err;
// }
// };
1 change: 1 addition & 0 deletions supabase/functions/_shared/supabase/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export const setSelectedIzbushka = async (
.eq("telegram_id", telegram_id)
.select("*");

console.log(data, "363 data setSelectedIzbushka")
if (error) {
throw new Error("Error setSelectedIzbushka: " + error);
}
Expand Down
4 changes: 4 additions & 0 deletions supabase/functions/_shared/telegram/bots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export const babaYagaChatId = Deno.env.get("AI_BABA_YAGA_CHAT_ID");

export const bugCatcherRequest = async (title: string, error: any) => {
try {
if (DEV) {
console.log(`👾 ${title}\n\n${JSON.stringify(error)}`)
return
}
if (babaYagaChatId) {
await bugCatcherDevBot.api.sendMessage(
babaYagaChatId,
Expand Down
Loading

0 comments on commit 2f2c699

Please sign in to comment.