/
appMessage.ts
86 lines (78 loc) · 2.34 KB
/
appMessage.ts
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
import { Handler } from '@netlify/functions';
import axios from 'axios';
import dailyAPIUrl from './util';
// This handler sends an `"app-message"` even to
// the specified room name and participant(s).
// https://docs.daily.co/reference/rest-api/rooms/send-app-message
const handler: Handler = async (event, _context) => {
const apiKey = process.env.DAILY_API_KEY;
if (!apiKey) {
console.error('Daily API key is missing');
return {
statusCode: 500,
body: 'An internal server error occurred',
};
}
// We expect a room name to have been passed as a query parameter
const params = event.queryStringParameters;
const roomName = params?.roomName;
if (!roomName) {
return {
statusCode: 400,
body: 'Query parameters must contain a Daily room name',
};
}
// The recipient parameter is optional. The default
// will send a message to all participants.
const recipient = params?.to;
try {
await sendAppMessage(apiKey, roomName, recipient);
return {
statusCode: 200,
};
} catch (e) {
return {
statusCode: 500,
body: String(e),
};
}
};
// sendAppMessage() uses Daily's REST API to send an `"app-message"`
// event to one or all Daily participant clients.
// Docs: https://docs.daily.co/reference/rest-api/rooms/send-app-message
async function sendAppMessage(
apiKey: string,
roomName: string,
recipient: string = '*'
) {
// Prepare request body.
// We include the recipient twice because the property
// in "data" will be accessible through the `"app-message"`
// event payload. The recipient can use it to tailor their
// message handling based on whether they were the only recipient.
const req = {
data: {
recipient,
},
recipient,
};
const data = JSON.stringify(req);
// Prepare headers containing Daily API key
const headers = {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
};
const url = `${dailyAPIUrl}/rooms/${roomName}/send-app-message`;
const errMsg = 'failed to send app message';
const res = await axios.post(url, data, { headers });
if (res.status !== 200 || !res.data) {
console.error(
'Unexpected app message endpoint response:',
res.status,
res.data
);
throw new Error(errMsg);
}
}
// eslint-disable-next-line import/prefer-default-export
export { handler };