Use Twilio SDK channelsSenders.list to discover sender
This commit is contained in:
17
src/index.ts
17
src/index.ts
@@ -476,17 +476,20 @@ async function findWhatsappSenderSid(
|
|||||||
return explicitSenderSid;
|
return explicitSenderSid;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const resp = await (client as unknown as TwilioRequester).request({
|
// Prefer official SDK list helper to avoid request-shape mismatches.
|
||||||
method: 'get',
|
// Twilio helper types are broad; we narrow to expected shape.
|
||||||
uri: 'https://messaging.twilio.com/v2/Channels/Senders',
|
const senders = (await (client as any).messaging.v2.channelsSenders.list({
|
||||||
params: { Channel: 'whatsapp', PageSize: 50 }
|
channel: 'whatsapp',
|
||||||
});
|
pageSize: 50
|
||||||
const senders = resp.data?.senders;
|
})) as Array<{ sid?: string; senderId?: string; sender_id?: string }>;
|
||||||
if (!senders) {
|
if (!senders) {
|
||||||
throw new Error('List senders response missing "senders" array');
|
throw new Error('List senders response missing "senders" array');
|
||||||
}
|
}
|
||||||
const match = senders.find(
|
const match = senders.find(
|
||||||
(s) => typeof s?.sender_id === 'string' && s.sender_id === withWhatsAppPrefix(from)
|
(s) =>
|
||||||
|
typeof (s as any)?.senderId === 'string'
|
||||||
|
? (s as any).senderId === withWhatsAppPrefix(from)
|
||||||
|
: typeof (s as any)?.sender_id === 'string' && (s as any).sender_id === withWhatsAppPrefix(from)
|
||||||
);
|
);
|
||||||
if (!match || typeof match.sid !== 'string') {
|
if (!match || typeof match.sid !== 'string') {
|
||||||
throw new Error(`Could not find sender ${withWhatsAppPrefix(from)} in Twilio account`);
|
throw new Error(`Could not find sender ${withWhatsAppPrefix(from)} in Twilio account`);
|
||||||
|
|||||||
Reference in New Issue
Block a user