Advertisement
Guest User

Untitled

a guest
Apr 14th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. <script setup>
  2. import { ref, onMounted } from 'vue'
  3. import Vapi from '@vapi-ai/web'
  4. import debug from '~/composables/console_'
  5. import { get_runtime_config } from '~/composables/synaptic_'
  6.  
  7. let vapi = ref(null)
  8. let connected = ref(false)
  9. let connecting = ref(false)
  10.  
  11. const assistant_id = 'b18f2895-495c-4eec-bcab-55e7cf52c1d6'
  12. const VAPI_PUBLIC_API_KEY = get_runtime_config()?.public?.VAPI_PUBLIC_API_KEY
  13.  
  14. const init_vapi = () => {
  15. if (!VAPI_PUBLIC_API_KEY) return
  16. vapi.value = new Vapi(VAPI_PUBLIC_API_KEY)
  17.  
  18. vapi.value.on('call-start', () => {
  19. connecting.value = false
  20. connected.value = true
  21. })
  22.  
  23. vapi.value.on('call-end', () => {
  24. connecting.value = false
  25. connected.value = false
  26. })
  27.  
  28. vapi.value.on('error', (error) => {
  29. debug.error('VAPI error:', error)
  30. connecting.value = false
  31. })
  32. }
  33.  
  34. const start_call = async () => {
  35. if (!vapi.value) return
  36. connecting.value = true
  37. try {
  38. await vapi.value.start(assistant_id)
  39. } catch (error) {
  40. debug.error('Start call error:', error)
  41. connecting.value = false
  42. }
  43. }
  44.  
  45. const end_call = () => {
  46. if (!vapi.value || !connected.value) return
  47. vapi.value.stop()
  48. }
  49.  
  50. onMounted(() => {
  51. init_vapi()
  52. })
  53. </script>
  54.  
  55. <template>
  56. <div>
  57. <button v-if="!connected" @click="start_call" :disabled="connecting">
  58. {{ connecting ? 'Connecting...' : 'Start Call' }}
  59. </button>
  60. <button v-else @click="end_call">
  61. End Call
  62. </button>
  63. </div>
  64. </template>
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement