Untitled

mail@pastecode.io avatar
unknown
javascript
a month ago
1.7 kB
7
Indexable
Never
const dotenv = require('dotenv');
const fetch = require('node-fetch');
const { updateConnectionMetadata, getConnections } = require('./wsManager');

dotenv.config();

const API_KEY = process.env.DIDAPIKEY;

async function generateTalk(script, clientId) {


    const url = process.env.TALKAPIURL;
  const options = {
    method: 'POST',
    headers: {
      accept: 'application/json',
      'content-type': 'application/json',
      Authorization: `Basic ${Buffer.from(`${API_KEY}:`).toString('base64')}`
    },
    body: JSON.stringify({
      source_url: process.env.AVATARIMAGE,
      script: {
        type: "text",
        ssml: true,
        input: script,
        provider: {
          type: "microsoft",
          voice_id: "en-US-JennyNeural",
          voice_config: { style: "Cheerful" },
        },
      },
      output_format: 'mov',
      webhook: process.env.TALKWEBHOOK,
      user_data: clientId,
      presenter_id: "amy-jcwCkr1grs",
      driver_id: "uM00QMwJ9x"
    })
  };

  try {
    const response = await fetch(url, options);
    const data = await response.json();

    if (response.ok) {
      const connection = getConnections().find(conn => conn.clientId === clientId);
      if (connection) {
        updateConnectionMetadata({ clientId: clientId, talk_status: data.status, talkId: data.id });
      }
      return data;
    } else {
      console.error(`\n\nD-ID API Error: ${response.status} - ${response.statusText}`);
      return { error: `API Error: ${response.statusText}` };
    }
  } catch (error) {
    console.error('\n\nError in generateTalk:', error);
    return { error: error.message };
  }


}

module.exports = generateTalk;
Leave a Comment