Guest User

Untitled

a guest
May 22nd, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1.  
  2. import Vapi from '@vapi-ai/web';
  3. import React, { useEffect, useState } from "react";
  4.  
  5. export const VapiButtonTYG = () => {
  6. const vapi = new Vapi('')
  7.  
  8. const [vapiState, setVapiState] = useState('stopped');
  9. const [vapiLoading, setVapiLoading] = useState(false);
  10.  
  11. vapi.on('speech-start', () => {
  12. console.log('Speech has started');
  13. // setVapiLoading(false);
  14. });
  15.  
  16. vapi.on('speech-end', () => {
  17. console.log('Speech has ended');
  18. });
  19.  
  20. vapi.on('call-start', () => {
  21. console.log('Call has started');
  22. });
  23.  
  24. vapi.on('call-end', () => {
  25. console.log('Call has stopped');
  26. // setVapiState('stopped');
  27. });
  28.  
  29. vapi.on('volume-level', (volume) => {
  30. console.log(`Assistant volume level: ${volume}`);
  31. });
  32.  
  33. // Function calls and transcripts will be sent via messages
  34. vapi.on('message', (message) => {
  35. console.log(message);
  36. });
  37.  
  38. vapi.on('error', (e) => {
  39. console.error(e);
  40. });
  41.  
  42.  
  43.  
  44. return (
  45. <>
  46.  
  47. {vapiLoading && (
  48. <svg className="animate-spin -ml-1 mr-3 h-5 w-5 text-blue-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
  49. <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
  50. <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
  51. </svg>
  52. )}
  53.  
  54. {/* {vapiState === 'stopped' && ( */}
  55. <button
  56. onClick={() => {
  57. // setVapiLoading(true);
  58. vapi.start('9a910528-2101-45b6-a6fe-314a5afcf9a2');
  59. // setVapiState('started');
  60. }}
  61. className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
  62. >
  63.  
  64. {vapiLoading ? 'Loading...' : 'VapiAi Start TYG'}
  65. </button>
  66. {/* )} */}
  67.  
  68. {/* {vapiState === 'started' && ( */}
  69. <button
  70. onClick={() => {
  71. // console.log('Stopping VapiAi TYG')
  72. vapi.stop();
  73.  
  74. // setVapiState('stopped');
  75. }}
  76. className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded"
  77. >
  78. {vapiLoading ? 'Loading...' : 'VapiAi Stop TYG'}
  79. </button>
  80. {/* )} */}
  81.  
  82. </>
  83. );
  84. };
Advertisement
Add Comment
Please, Sign In to add comment