UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 1.93 kB
import{resolveTwilioAuthToken}from"#public/channels/twilio/verify.js";function twilioContinuationToken(e,t){return`${e}:${t??``}`}async function resolveTwilioAccountSid(e){let t=e??process.env.TWILIO_ACCOUNT_SID;if(!t)throw Error(`TWILIO_ACCOUNT_SID is required.`);return typeof t==`function`?await t():t}async function callTwilioApi(t){let n=await resolveTwilioAccountSid(t.credentials?.accountSid),r=await resolveTwilioAuthToken(t.credentials?.authToken),i=t.fetch??fetch,a=`${t.apiBaseUrl??`https://api.twilio.com`}${t.path}`,o=encodeForm(t.body),s=await i(a,{method:`POST`,headers:{authorization:`Basic ${Buffer.from(`${n}:${r}`).toString(`base64`)}`,"content-type":`application/x-www-form-urlencoded;charset=UTF-8`},body:o});return{status:s.status,ok:s.ok,body:await parseResponseBody(s)}}async function sendTwilioMessage(e){if(!e.from&&!e.messagingServiceSid)throw Error(`twilioChannel: sending a message requires from or messagingServiceSid.`);let t=await resolveTwilioAccountSid(e.credentials?.accountSid);return callTwilioApi({apiBaseUrl:e.apiBaseUrl,credentials:e.credentials,fetch:e.fetch,path:`/2010-04-01/Accounts/${encodeURIComponent(t)}/Messages.json`,body:{Body:e.body,From:e.from,MessagingServiceSid:e.messagingServiceSid,StatusCallback:e.statusCallbackUrl,To:e.to}})}async function updateTwilioCall(e){let t=await resolveTwilioAccountSid(e.credentials?.accountSid);return callTwilioApi({apiBaseUrl:e.apiBaseUrl,credentials:e.credentials,fetch:e.fetch,path:`/2010-04-01/Accounts/${encodeURIComponent(t)}/Calls/${encodeURIComponent(e.callSid)}.json`,body:{Twiml:e.twiml}})}function encodeForm(e){let t=new URLSearchParams;for(let[n,r]of Object.entries(e))r!=null&&t.set(n,String(r));return t}async function parseResponseBody(e){let t=await e.text();if(!t)return null;try{return JSON.parse(t)}catch{return t}}export{callTwilioApi,resolveTwilioAccountSid,sendTwilioMessage,twilioContinuationToken,updateTwilioCall};