forked from k-kochhar/SafeCall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook-test-script.js
More file actions
159 lines (146 loc) · 3.89 KB
/
webhook-test-script.js
File metadata and controls
159 lines (146 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Test script to demonstrate how to send data to the SafeCall webhook
*
* This script simulates sending transcription data to the webhook.
* It can be used as a reference for integrating with the webhook.
*
* Usage:
* 1. Install Node.js
* 2. Run: node webhook-test-script.js
*/
// Replace with your actual webhook URL
const WEBHOOK_URL = 'http://localhost:3000/api/webhook';
// Generate unique IDs based on timestamp
const baseId = Date.now();
// Sample transcription data
const sampleData = {
transcriptions: [
{
id: baseId,
speaker: 'Caller',
text: 'Hey, I need you to come pick me up right now.',
time: '00:05',
sentiment: 'urgent'
},
{
id: baseId + 1,
speaker: 'You',
text: 'What\'s going on? Are you okay?',
time: '00:08',
sentiment: 'concerned'
},
{
id: baseId + 2,
speaker: 'Caller',
text: 'I\'m at the party we talked about, but I don\'t feel safe here. Can you come get me?',
time: '00:15',
sentiment: 'anxious'
}
],
insights: [
{
id: baseId + 100,
type: 'warning',
text: 'Detected anxiety in caller\'s voice'
},
{
id: baseId + 101,
type: 'info',
text: 'Location shared: 1234 Main Street'
},
{
id: baseId + 102,
type: 'alert',
text: 'Keywords detected: "don\'t feel safe"'
}
]
};
/**
* Function to send data to the webhook
*/
async function sendToWebhook(data) {
try {
const response = await fetch(WEBHOOK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
const result = await response.json();
console.log('Webhook response:', result);
return result;
} catch (error) {
console.error('Error sending to webhook:', error);
throw error;
}
}
/**
* Function to simulate a real-time conversation
*/
async function simulateConversation() {
console.log('Starting simulated conversation...');
// Send initial data
await sendToWebhook(sampleData);
console.log('Sent initial data');
// Add a new message after 3 seconds
setTimeout(async () => {
const newMessageId = Date.now();
const updatedData = {
transcriptions: [
{
id: newMessageId,
speaker: 'You',
text: 'I\'ll be there in 10 minutes. Stay on the phone with me.',
time: '00:20',
sentiment: 'supportive'
}
],
insights: []
};
await sendToWebhook(updatedData);
console.log('Sent update 1');
// Add another message after 3 more seconds
setTimeout(async () => {
const finalMessageId = Date.now();
const finalData = {
transcriptions: [
{
id: finalMessageId,
speaker: 'Caller',
text: 'Thank you. I\'ll wait by the front entrance.',
time: '00:25',
sentiment: 'relieved'
}
],
insights: [
{
id: finalMessageId + 100,
type: 'info',
text: 'Caller\'s stress level decreasing'
}
]
};
await sendToWebhook(finalData);
console.log('Sent update 2');
console.log('Simulation complete');
}, 3000);
}, 3000);
}
// Run the simulation
simulateConversation().catch(console.error);
/**
* Example of how to integrate this with your pipeline:
*
* 1. When new transcription data is available:
* sendToWebhook({
* transcriptions: [{ id: Date.now(), speaker: '...', text: '...' }],
* insights: [{ id: Date.now() + 100, type: '...', text: '...' }]
* });
*
* 2. The webhook accepts any valid JSON, so you can customize
* the data structure as needed.
*
* 3. The SafeCall app will accumulate all transcriptions and insights
* and display them in chronological order.
*/