How to use the Lists API to send to Slack, Inbox, and Email (NodeJS)
We are using the '@trycourier/courier'
node.js sdk within our Next.js/TypeScript app.
We would like to use the Lists API and Profiles API to send a Slack, Inbox, and Email message when a project status has changed in our app.
- How can I get the
b)
code below to send a Slack, Inbox, and Email to the Profiles in a List? - Should I be using
courier.lists.send()
orcourier.send()
when working with Lists? - Would it be better to use the Audience Object instead of the Lists API for what I am trying to do?
Example Code
Example of a List ID: 'organizationId.project.status_changed'
I created a list and profile with the following code:
const userId = req.body.user.id;
const { status } = await courier.mergeProfile({
recipientId: userId,
profile: {
email: req.body.user.email,
given_name: req.body.user.given_name,
courier: { channel: userId },
custom: {},
},
});
// listId = 'organizationId.project.status_changed'
await courier.lists.subscribe(req.body.listId, userId);
a) This code works to send a direct message via Slack, Inbox, and users email:
import { CourierClient } from '@trycourier/courier';
import { NextApiRequest, NextApiResponse } from 'next';
const slackBotUserOAuthToken = '---';
const courierTestKey = '---';
const courier = CourierClient({ authorizationToken: courierTestKey });
export default async (req: NextApiRequest, res: NextApiResponse): Promise<void> => {
const courierResponse = await courier.send({
message: {
to: [
{ slack: { access_token: slackBotUserOAuthToken, email: req.body.email } },
{ courier: { channel: req.body.userId } },
{ email: req.body.email },
],
content: { title: 'Project Status Change', body: 'Some text.' },
routing: { method: 'all', channels: ['direct_message', 'push', 'email'] },
},
});
res.status(201).json(courierResponse);
};
b) This code only sends an email to the Profile in a List and not to Slack or the Inbox:
// listId = 'organizationId.project.status_changed'
const courierTestKey = '---';
const courier = CourierClient({ authorizationToken: courierTestKey });
export default async (req: INextApiRequest<IListEvent>, res: NextApiResponse<IResponseData>): Promise<void> => {
// This code only sends to the profile email:
const response = await courier.send({
message: {
to: [{ list_id: req.body.listId }],
content: { title: 'Project Status Change', body: 'Some text.' },
},
routing: { method: 'all', channels: ['direct_message', 'push', 'email'] },
});
// This code doesn't do anything:
// const response = await courier.lists.send({
// event: 'COURIER_LIVE_ALERT',
// list: req.body.listId,
// data: { title: 'Project Status Change', body: 'Some text.' },
// });
res.status(201).json({});
};