@apostlejs/whatsapp 0.0.15 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";function e(e){return e&&e.__esModule?e:{default:e}}var t=e(require("node:crypto")),a=Object.defineProperty,r=(e,t)=>{for(var r in t)a(e,r,{get:t[r],enumerable:!0})},n={};r(n,{WhatsappErrorCode:()=>l,endpoints:()=>c,flowsEndpoints:()=>o,messagesEndpoints:()=>s,wabaEndpoints:()=>i});var o={create:{url:"/{waba_id}/flows",method:"post",headers:{"Content-Type":"application/json"},request:{body:null},response:null},updateMetadata:{url:"/{flow_id}",method:"post",headers:{"Content-Type":"application/json"},request:{body:null},response:null},readMany:{url:"/{waba_id}/flows",method:"get",response:null},delete:{url:"/{flow_id}",method:"delete",response:null},read:{url:"/{flow_id}",method:"get",request:{query:null},response:null},updateJson:{url:"/{flow_id}/assets",method:"post",request:{body:null},response:null},getPreview:{url:"/{flow_id}?fields=preview.invalidate(false)",method:"get",response:null},publish:{url:"/{flow_id}/publish",method:"post",response:null}},s={send:{url:"/{number_id}/messages",method:"post",headers:{"Content-Type":"application/json"},request:{body:null},response:null}},i={updateEncryption:{url:"/{number_id}/whatsapp_business_encryption",method:"post",headers:{"Content-Type":"application/x-www-form-urlencoded"},request:{body:null}},registerNumber:{url:"/{number_id}/register",method:"post",headers:{"Content-Type":"application/json"},request:{body:null}}},c={flows:o,messages:s,waba:i},l=(e=>(e[e.e0AuthException=0]="e0AuthException",e[e.e3ApiMethod=3]="e3ApiMethod",e[e.e10PermissionDenied=10]="e10PermissionDenied",e[e.e190AccessTokenExpired=190]="e190AccessTokenExpired",e[e.e2xxApiPermission=200]="e2xxApiPermission",e[e.e368TemporarilyBlockedForPolicyViolations=368]="e368TemporarilyBlockedForPolicyViolations",e[e.e130497BusinessAccountRestrictedByCountry=130497]="e130497BusinessAccountRestrictedByCountry",e[e.e131031AccountLocked=131031]="e131031AccountLocked",e[e.e1ApiUnknown=1]="e1ApiUnknown",e[e.e2ApiService=2]="e2ApiService",e[e.e33ParameterValueNotValid=33]="e33ParameterValueNotValid",e[e.e100InvalidParameter=100]="e100InvalidParameter",e[e.e130472UserNumberPartOfExperiment=130472]="e130472UserNumberPartOfExperiment",e[e.e131000SomethingWentWrong=131e3]="e131000SomethingWentWrong",e[e.e131005AccessDenied=131005]="e131005AccessDenied",e[e.e131008RequiredParameterMissing=131008]="e131008RequiredParameterMissing",e[e.e131009ParameterValueNotValid=131009]="e131009ParameterValueNotValid",e[e.e131016ServiceUnavailable=131016]="e131016ServiceUnavailable",e[e.e131021RecipientCannotBeSender=131021]="e131021RecipientCannotBeSender",e[e.e131026MessageUndeliverable=131026]="e131026MessageUndeliverable",e[e.e131030RecipientNotAllowedInList=131030]="e131030RecipientNotAllowedInList",e[e.e131037DisplayNameApprovalNeeded=131037]="e131037DisplayNameApprovalNeeded",e[e.e131042BusinessEligibilityPaymentIssue=131042]="e131042BusinessEligibilityPaymentIssue",e[e.e131045IncorrectCertificate=131045]="e131045IncorrectCertificate",e[e.e131047ReEngagementMessage=131047]="e131047ReEngagementMessage",e[e.e131049MetaChoseNotToDeliver=131049]="e131049MetaChoseNotToDeliver",e[e.e131050UserStoppedMarketingMessages=131050]="e131050UserStoppedMarketingMessages",e[e.e131051UnsupportedMessageType=131051]="e131051UnsupportedMessageType",e[e.e131052MediaDownloadError=131052]="e131052MediaDownloadError",e[e.e131053MediaUploadError=131053]="e131053MediaUploadError",e[e.e131057AccountInMaintenanceMode=131057]="e131057AccountInMaintenanceMode",e[e.e132000TemplateParamCountMismatch=132e3]="e132000TemplateParamCountMismatch",e[e.e132001TemplateDoesNotExist=132001]="e132001TemplateDoesNotExist",e[e.e132005TemplateHydratedTextTooLong=132005]="e132005TemplateHydratedTextTooLong",e[e.e132007TemplateFormatCharacterPolicyViolated=132007]="e132007TemplateFormatCharacterPolicyViolated",e[e.e132012TemplateParameterFormatMismatch=132012]="e132012TemplateParameterFormatMismatch",e[e.e132015TemplateIsPaused=132015]="e132015TemplateIsPaused",e[e.e132016TemplateIsDisabled=132016]="e132016TemplateIsDisabled",e[e.e132068FlowIsBlocked=132068]="e132068FlowIsBlocked",e[e.e132069FlowIsThrottled=132069]="e132069FlowIsThrottled",e[e.e133000IncompleteDeregistration=133e3]="e133000IncompleteDeregistration",e[e.e133004ServerTemporarilyUnavailable=133004]="e133004ServerTemporarilyUnavailable",e[e.e133005TwoStepVerificationPinMismatch=133005]="e133005TwoStepVerificationPinMismatch",e[e.e133006PhoneNumberReVerificationNeeded=133006]="e133006PhoneNumberReVerificationNeeded",e[e.e133008TooManyTwoStepVerificationPinGuesses=133008]="e133008TooManyTwoStepVerificationPinGuesses",e[e.e133009TwoStepVerificationPinGuessedTooFast=133009]="e133009TwoStepVerificationPinGuessedTooFast",e[e.e133010PhoneNumberNotRegistered=133010]="e133010PhoneNumberNotRegistered",e[e.e133015PhoneNumberDeletionPending=133015]="e133015PhoneNumberDeletionPending",e[e.e134011PaymentsTermsNotAccepted=134011]="e134011PaymentsTermsNotAccepted",e[e.e135000GenericUserError=135e3]="e135000GenericUserError",e[e.e2593107SynchronizationRequestLimitExceeded=2593107]="e2593107SynchronizationRequestLimitExceeded",e[e.e2593108SynchronizationRequestOutsideTimeWindow=2593108]="e2593108SynchronizationRequestOutsideTimeWindow",e[e.e4ApiTooManyCalls=4]="e4ApiTooManyCalls",e[e.e80007RateLimitIssues=80007]="e80007RateLimitIssues",e[e.e130429RateLimitHit=130429]="e130429RateLimitHit",e[e.e131048SpamRateLimitHit=131048]="e131048SpamRateLimitHit",e[e.e131056BusinessConsumerPairRateLimitHit=131056]="e131056BusinessConsumerPairRateLimitHit",e[e.e133016AccountRegisterDeregisterRateLimitExceeded=133016]="e133016AccountRegisterDeregisterRateLimitExceeded",e))(l||{}),p={};r(p,{actions:()=>x,createContactUrl:()=>O,flows:()=>S,parsers:()=>L,security:()=>H,toGraphLanguageTag:()=>I,utils:()=>k});var d={};r(d,{create:()=>_,delete:()=>E,get:()=>b,getMany:()=>v,getPreview:()=>g,publish:()=>T,updateJson:()=>w,updateMetadata:()=>h});var u={};r(u,{settings:()=>m});var f={},m={setup:e=>{Object.assign(f,e)},get:e=>{const t=f[e]??process.env[e];if(!t)throw new Error(`Missing environment variable: ${e}`);return t}};async function y(e,t,a){const r=`https://graph.facebook.com/v${m.get("GRAPH_API_VERSION")}`,n={Authorization:`Bearer ${m.get("META_APP_ACCESS_TOKEN")}`};let o=e.url;const s={...n,...e.headers,...t.headers},i=new URLSearchParams(t.query).toString();"/"===o[0]&&(o=o.slice(1)),o=`${r}/${o}`,i&&(o=`${o}?${i}`);const c={waba_id:m.get("WHATSAPP_ACCOUNT_ID"),number_id:m.get("WHATSAPP_NUMBER_ID"),...t.params};for(const e in c)o=o.replace(`{${e}}`,c[e]);const l=t.body;let p;if(l&&a?.asFormData){const e=new FormData;for(const t in l)e.append(t,l[t]);p=e}else l&&a?.asUrlEncoded?p=new URLSearchParams(l).toString():l&&(p=JSON.stringify(l));return await fetch(o,{method:e.method,headers:s,body:p}).then((e=>e.json())).then((e=>{if("error"in e)throw{response:{data:e.error}};return e})).catch((async e=>{const t=e.response.data.message,a=e.response.data.error_data?.details||"",r=t.match(/^\(#\d+\)/),n=r?r[0]:"",o=t.replace(/^\(#\d+\)\s*/,""),s=a?a.startsWith(o)?`${n} ${a}`:`${t}: ${a}`:t;e.response.data.message=s;const{message:i,...c}=e.response.data;throw new Error(i,{cause:c})}))}async function _(e){const t=c.flows.create,a=e.flow_json?JSON.stringify(e.flow_json,null,2):void 0;return await y(t,{body:{name:e.name,categories:e.categories,clone_flow_id:e.clone_flow_id,endpoint_uri:e.endpoint_uri,publish:e.publish,flow_json:a}})}async function h(e,t){const a=c.flows.updateMetadata;return await y(a,{body:t,params:{flow_id:e}})}async function w(e,t){const a=c.flows.updateJson,r={name:"flow.json",asset_type:"FLOW_JSON",file:new Blob([JSON.stringify(t,null,2)],{type:"application/json"})};return await y(a,{params:{flow_id:e},body:r},{asFormData:!0}).catch((e=>{if(e.error_user_msg.startsWith("Flow JSON has been saved"))return{success:!0,validation_errors:[]};throw e}))}async function g(e,t){const a=c.flows.getPreview,{preview:r}=await y(a,{params:{flow_id:e}}),n=r.preview_url.replaceAll("\\",""),o=Object.entries(t??{}).reduce(((e,[t,a])=>(e[t]="flow_action_payload"===t?encodeURIComponent(JSON.stringify(a)):a.toString(),e)),{});return`${n}&${new URLSearchParams(o).toString()}`}async function E(e){const t=c.flows.delete;return await y(t,{params:{flow_id:e}})}async function b(e,t){const a=c.flows.read;return await y(a,{params:{flow_id:e},query:{fields:t?.fields.join(",")??""}})}async function v(){const e=c.flows.readMany;return await y(e,{})}async function T(e){const t=c.flows.publish;return await y(t,{params:{flow_id:e}})}var P=e=>e.split("?")[0],S={createToken:({flow_name:e,flow_parameters:t,chatId:a})=>{const r=new URLSearchParams({chat_id:a});if(r.set("flow_identifier",crypto.randomUUID()),t)for(const[e,a]of Object.entries(t))r.set(e,a.toString());return`${e}?${decodeURIComponent(r.toString())}`},getName:P,destructureFlowToken:e=>{const t=P(e);let a=e.split("?")?.[1];"&"===a?.[0]&&(a=a.slice(1));const r=new URLSearchParams(a),n=r.get("chat_id")||r.get("chatId"),o=r.get("flow_identifier")||r.get("flowIdentifier"),s={};for(const[e,t]of r.entries())"chat_id"!==e&&"chatId"!==e&&"flow_identifier"!==e&&"flowIdentifier"!==e&&(s[e]=t);return{paramsString:a,flowName:t,chatId:n,flowIdentifier:o,flowParameters:s}}},N=(e=>(e.AR_SA="ar-SA",e.BN_BD="bn-BD",e.BN_IN="bn-IN",e.CS_CZ="cs-CZ",e.DA_DK="da-DK",e.DE_AT="de-AT",e.DE_CH="de-CH",e.DE_DE="de-DE",e.EL_GR="el-GR",e.EN_AU="en-AU",e.EN_CA="en-CA",e.EN_GB="en-GB",e.EN_IE="en-IE",e.EN_IN="en-IN",e.EN_NZ="en-NZ",e.EN_US="en-US",e.EN_ZA="en-ZA",e.ES_AR="es-AR",e.ES_CL="es-CL",e.ES_CO="es-CO",e.ES_ES="es-ES",e.ES_MX="es-MX",e.ES_US="es-US",e.FI_FI="fi-FI",e.FR_BE="fr-BE",e.FR_CA="fr-CA",e.FR_CH="fr-CH",e.FR_FR="fr-FR",e.HE_IL="he-IL",e.HI_IN="hi-IN",e.HU_HU="hu-HU",e.ID_ID="id-ID",e.IT_CH="it-CH",e.IT_IT="it-IT",e.JA_JP="ja-JP",e.KO_KR="ko-KR",e.NL_BE="nl-BE",e.NL_NL="nl-NL",e.NO_NO="no-NO",e.PL_PL="pl-PL",e.PT_BR="pt-BR",e.PT_PT="pt-PT",e.RO_RO="ro-RO",e.RU_RU="ru-RU",e.SK_SK="sk-SK",e.SV_SE="sv-SE",e.TA_IN="ta-IN",e.TA_LK="ta-LK",e.TH_TH="th-TH",e.TR_TR="tr-TR",e.ZH_CN="zh-CN",e.ZH_HK="zh-HK",e.ZH_TW="zh-TW",e))(N||{}),A={"ar-SA":"ar","bn-BD":"bn","bn-IN":"bn","cs-CZ":"cs","da-DK":"da","de-AT":"de","de-CH":"de","de-DE":"de","el-GR":"el","en-AU":"en","en-CA":"en","en-GB":"en_GB","en-IE":"en","en-IN":"en","en-NZ":"en","en-ZA":"en","en-US":"en_US","es-AR":"es_AR","es-CL":"es","es-CO":"es","es-ES":"es_ES","es-MX":"es_MX","es-US":"es","fi-FI":"fi","fr-BE":"fr","fr-CA":"fr","fr-CH":"fr","fr-FR":"fr","he-IL":"he","hi-IN":"hi","hu-HU":"hu","id-ID":"id","it-CH":"it","it-IT":"it","ja-JP":"ja","ko-KR":"ko","nl-BE":"nl","nl-NL":"nl","no-NO":"nb","pl-PL":"pl","pt-PT":"pt_PT","pt-BR":"pt_BR","ro-RO":"ro","ru-RU":"ru","sk-SK":"sk","sv-SE":"sv","ta-IN":"ta","ta-LK":"ta","th-TH":"th","tr-TR":"tr","zh-CN":"zh_CN","zh-HK":"zh_HK","zh-TW":"zh_TW"},I=e=>{const t=A[e];return void 0===t?"en":t},R=e=>{const t={to:e.to,messaging_product:"whatsapp",recipient_type:"individual"};return e.reply&&(t.context={message_id:e.reply}),e.showUrlPreviewImage&&(t.preview_url=e.showUrlPreviewImage),t},C={flow:e=>{if("flow"!==e.message.type)throw new Error("Invalid type");const{message:t,...a}=e,{flow:r,...n}=t,o=R(a);r.mode||(r.mode=m.get("WHATSAPP_FLOWS_MODE"));const s=S.createToken({chatId:o.to,flow_name:r.name,flow_parameters:r.parameters});return{type:"interactive",...o,interactive:{...n,type:"flow",action:{name:"flow",parameters:{flow_name:r.name,flow_token:s,flow_message_version:"3",flow_cta:r.button,flow_action:r.action??"navigate",mode:r.mode,...r.payload&&{flow_action_payload:{...r.payload,screen:r.payload.screen}}}}}}},text:e=>{if("text"!==e.message.type)throw new Error("Invalid type");const{message:{text:t},...a}=e,r={type:"text",text:{body:t},...R(a)};return e.message.previewUrl&&r.text&&(r.text.preview_url=e.message.previewUrl),r},list:e=>{if("list"!==e.message.type)throw new Error("Invalid type");const{message:t,...a}=e,{list:r,type:n,...o}=t;return{...R(a),type:"interactive",interactive:{...o,type:n,action:r}}},button:e=>{if("button"!==e.message.type)throw new Error("Invalid type");const{message:t,...a}=e,{buttons:r,type:n,...o}=t;return{...R(a),type:"interactive",interactive:{...o,type:n,action:{buttons:r.map((e=>({reply:{id:e.id,title:e.text},type:"reply"})))}}}},template:e=>{if("template"!==e.message.type)throw new Error("Invalid type");const{message:{type:t,...a},...r}=e,n={...R(r),type:t,template:{...a,language:{code:I(a.language)}}};return n.template&&(n.template.namespace=process.env.WHATSAPP_MESSAGE_NAMESPACE),n},media:e=>{if("media"!==e.message.type)throw new Error("Invalid type");const{message:{type:t,...a},...r}=e,n=Object.entries(a)[0],[o,{ref:s,...i}]=n,c=i,l=R(r);return Number.isNaN(Number(s))?c.link=s:c.id=s,{type:o,[o]:c,...l}},contact:e=>{if("contact"!==e.message.type)throw new Error("Invalid type");const{message:{contacts:t},...a}=e;return{type:"contacts",contacts:t,...R(a)}}};function U(e){const{encrypted_aes_key:a,encrypted_flow_data:r,initial_vector:n}=e,o=t.default.createPrivateKey({key:m.get("WHATSAPP_ACCOUNT_ENCRYPTION_PRIVATE_KEY"),passphrase:m.get("WHATSAPP_ACCOUNT_ENCRYPTION_PASSPHRASE")}),s=t.default.privateDecrypt({key:o,padding:t.default.constants.RSA_PKCS1_OAEP_PADDING,oaepHash:"sha256"},Buffer.from(a,"base64")),i=Buffer.from(r,"base64"),c=Buffer.from(n,"base64"),l=i.subarray(0,-16),p=i.subarray(-16),d=t.default.createDecipheriv("aes-128-gcm",s,c);d.setAuthTag(p);const u=Buffer.concat([d.update(l),d.final()]).toString("utf-8");return{payload:JSON.parse(u),encryptionMetadata:{aesKeyBuffer:s,initialVectorBuffer:c}}}function B(e){const t=new URL(e.url).searchParams,a=t.get("hub.verify_token"),r=t.get("hub.challenge");return!(!a||!r)&&a===m.get("WHATSAPP_WEBHOOK_KEY")&&r}async function M(e,a){const r=e.headers.get("x-hub-signature-256");if(!r)return!1;const n=r.replace("sha256=",""),o=t.default.createHmac("sha256",m.get("WHATSAPP_WEBHOOK_KEY")).update(a,"utf-8").digest("hex");return!!t.default.timingSafeEqual(Buffer.from(n),Buffer.from(o))}var L={toGraph:{sendMessage:e=>C[e.message.type](e),flowResponse:(e,t)=>{if(!t.data)throw new Error("Missing data in flow response");if(!t.screen)throw new Error("Missing screen in flow response");if("SUCCESS"===t.screen){const a=t.data??{};t.data={extension_message_response:{params:{flow_token:e,...a}}}}return t}},toSDK:{webhook:async function(e){if("GET"===e.method){const t=B(e);if(!t)throw new Error("Invalid health check hub");return{type:"healthCheck",payload:t}}const t=await e.text(),a=JSON.parse(t);if("entry"in a){if(!M(e,t))throw new Error("Invalid application webhook signature");const n=a.entry.flatMap((e=>e.changes)).filter((e=>"messages"===e.field)).flatMap((e=>e.value.messages)).filter(Boolean).map((e=>function(e){const t=e.from,a={id:e.id,type:e.type,timestamp:e.timestamp,metadata:{forwarded:e.context?.forwarded,frequentlyForwarded:e.context?.frequently_forwarded}},r=function(e){if(!(e.audio||e.document||e.video||e.image))return null;const t=e.audio?.id??e.document?.id??e.image?.id??e.video?.id,a=e.audio?.mime_type??e.document?.mime_type??e.image?.mime_type??e.video?.mime_type,r=["audio","document","image","video"].find((t=>e[t])),n=e.image?.sha256??e.document?.sha256??e.video?.sha256;return{id:t,type:r,caption:e.document?.caption??e.image?.caption??e.video?.caption??null,mime_type:a,sha256:n,filename:e.document?.filename??e.video?.filename}}(e),n=function(e){return e.text?.body??e.button?.text??e.interactive?.button_reply?.title??e.interactive?.list_reply?.title??e.document?.caption??null}(e),o=function(e){return e.interactive?{...e.interactive?.button_reply||e.button?{button:{id:e.interactive?.button_reply?.id??null,title:e.button?.text??e.interactive.button_reply?.title,payload:e.button?.payload??null}}:{},...e.interactive.list_reply?{selectedOption:{id:e.interactive.list_reply.id,title:e.interactive.list_reply.title,description:e.interactive.list_reply.description}}:{},...e.interactive.nfm_reply?{flowResponse:JSON.parse(e.interactive.nfm_reply.response_json)}:{}}:null}(e);return{...a,...o&&{interaction:o},...r&&{media:r},text:n,chatId:t}}(e)));return{type:"application",payload:{messageReceived:(r=n,r.length?r:void 0)}}}var r;if("encrypted_flow_data"in a)return{type:"flowExchange",payload:{...U(a),pingResponse:{data:{status:"active"}}}};throw new Error("Unrecognized event")}}},x={messages:{send:async function(e){const t=L.toGraph.sendMessage(e),a=c.messages.send;return await y(a,{body:t})}},flows:{...d},waba:{registerNumber:async function({dataRegion:e,pin:t}){const a=c.waba.registerNumber,r={messaging_product:"whatsapp",pin:t};return e&&(r.data_localization_region=e),await y(a,{body:r},{asUrlEncoded:!0})},encryption:{upload:async function(e){const t=c.waba.updateEncryption,a={business_public_key:e};return await y(t,{body:a},{asUrlEncoded:!0})}}}};async function D(e){return await fetch(e).then((async e=>{const t=await e.arrayBuffer();return Buffer.from(t)}))}var H={verifyHub:B,verifySignature:M,generateWabaEncryption:async function(){const e=t.default.randomUUID(),{publicKey:a,privateKey:r}=t.default.generateKeyPairSync("rsa",{modulusLength:2048,publicKeyEncoding:{type:"spki",format:"pem"},privateKeyEncoding:{type:"pkcs8",format:"pem",cipher:"aes-256-cbc",passphrase:e}});return{passphrase:e,publicKey:a,privateKey:r}},decryptFlowBody:U,encryptFlowResponse:function(e,a){const r=[];for(const e of Buffer.from(a.initialVectorBuffer).entries())r.push(~e[1]);const n=t.default.createCipheriv("aes-128-gcm",Buffer.from(a.aesKeyBuffer),Buffer.from(r));return Buffer.concat([n.update(JSON.stringify(e),"utf-8"),n.final(),n.getAuthTag()]).toString("base64")},decryptFlowMedia:async function(e,a="https://picsum.photos/seed/picsum/200"){let{cdn_url:r,encryption_metadata:n}=e;if("EXAMPLE_DATA__CDN_URL_WILL_COME_IN_THIS_FIELD"===r)return r=a,await D(r);const o=await D(r),{iv:s,encryption_key:i,hmac_key:c,encrypted_hash:l,plaintext_hash:p}=n,d={iv:Buffer.from(s,"base64"),encryption_key:Buffer.from(i,"base64"),hmac_key:Buffer.from(c,"base64")};if(t.default.createHash("sha256").update(o).digest("base64")!==l)throw new Error("Encrypted hash validation failed");const u=o.subarray(0,o.length-10),f=o.subarray(-10);if(!t.default.createHmac("sha256",d.hmac_key).update(Buffer.concat([d.iv,u])).digest().subarray(0,10).equals(f))throw new Error("HMAC validation failed");const m=t.default.createDecipheriv("aes-256-cbc",d.encryption_key,d.iv),y=m.update(u),_=Buffer.concat([y,m.final()]);if(t.default.createHash("sha256").update(_).digest("base64")!==p)throw new Error("Decrypted media hash validation failed");return _}},O=(e,t)=>{const a=new URL("/send","https://api.whatsapp.com/"),r=e.replace(/[()\-+ ]/g,"");return a.searchParams.append("phone",r),t&&a.searchParams.append("text",t),a.href},k={flows:S,createContactUrl:O},K=null;exports.BCP47LanguageTag=N,exports.WhatsappErrorCode=l,exports.actions=x,exports.createContactUrl=O,exports.createWhatsapp=()=>K||(K=(()=>{const{settings:e}=u,{actions:t,flows:a,parsers:r,security:o,utils:s,toGraphLanguageTag:i}=p,{endpoints:c}=n;return{settings:e,sdk:{actions:t,flows:a,parsers:r,security:o,utils:s,toGraphLanguageTag:i},graph:{endpoints:c}}})()),exports.endpoints=c,exports.flows=S,exports.flowsEndpoints=o,exports.messagesEndpoints=s,exports.parsers=L,exports.security=H,exports.toGraphLanguageTag=I,exports.utils=k,exports.wabaEndpoints=i;
1
+ "use strict";function e(e){return e&&e.__esModule?e:{default:e}}var t=e(require("node:crypto")),a=Object.defineProperty,r=(e,t)=>{for(var r in t)a(e,r,{get:t[r],enumerable:!0})},n={};r(n,{WhatsappErrorCode:()=>d,endpoints:()=>l,flowsEndpoints:()=>s,mediaEndpoints:()=>c,messagesEndpoints:()=>i,wabaEndpoints:()=>o});var s={create:{url:"/{waba_id}/flows",method:"post",headers:{"Content-Type":"application/json"},request:{body:null},response:null},updateMetadata:{url:"/{flow_id}",method:"post",headers:{"Content-Type":"application/json"},request:{body:null},response:null},readMany:{url:"/{waba_id}/flows",method:"get",response:null},delete:{url:"/{flow_id}",method:"delete",response:null},read:{url:"/{flow_id}",method:"get",request:{query:null},response:null},updateJson:{url:"/{flow_id}/assets",method:"post",request:{body:null},response:null},getPreview:{url:"/{flow_id}?fields=preview.invalidate(false)",method:"get",response:null},publish:{url:"/{flow_id}/publish",method:"post",response:null}},i={send:{url:"/{number_id}/messages",method:"post",headers:{"Content-Type":"application/json"},request:{body:null},response:null},showTypingIndicator:{url:"/{number_id}/messages",method:"post",headers:{"Content-Type":"application/json"},request:{body:null},response:null}},o={updateEncryption:{url:"/{number_id}/whatsapp_business_encryption",method:"post",headers:{"Content-Type":"application/x-www-form-urlencoded"},request:{body:null}},registerNumber:{url:"/{number_id}/register",method:"post",headers:{"Content-Type":"application/json"},request:{body:null}}},c={fetch:{url:"/{media_id}",method:"get",request:{query:null},response:null}},l={flows:s,messages:i,waba:o,media:c},d=(e=>(e[e.e0AuthException=0]="e0AuthException",e[e.e3ApiMethod=3]="e3ApiMethod",e[e.e10PermissionDenied=10]="e10PermissionDenied",e[e.e190AccessTokenExpired=190]="e190AccessTokenExpired",e[e.e2xxApiPermission=200]="e2xxApiPermission",e[e.e368TemporarilyBlockedForPolicyViolations=368]="e368TemporarilyBlockedForPolicyViolations",e[e.e130497BusinessAccountRestrictedByCountry=130497]="e130497BusinessAccountRestrictedByCountry",e[e.e131031AccountLocked=131031]="e131031AccountLocked",e[e.e1ApiUnknown=1]="e1ApiUnknown",e[e.e2ApiService=2]="e2ApiService",e[e.e33ParameterValueNotValid=33]="e33ParameterValueNotValid",e[e.e100InvalidParameter=100]="e100InvalidParameter",e[e.e130472UserNumberPartOfExperiment=130472]="e130472UserNumberPartOfExperiment",e[e.e131000SomethingWentWrong=131e3]="e131000SomethingWentWrong",e[e.e131005AccessDenied=131005]="e131005AccessDenied",e[e.e131008RequiredParameterMissing=131008]="e131008RequiredParameterMissing",e[e.e131009ParameterValueNotValid=131009]="e131009ParameterValueNotValid",e[e.e131016ServiceUnavailable=131016]="e131016ServiceUnavailable",e[e.e131021RecipientCannotBeSender=131021]="e131021RecipientCannotBeSender",e[e.e131026MessageUndeliverable=131026]="e131026MessageUndeliverable",e[e.e131030RecipientNotAllowedInList=131030]="e131030RecipientNotAllowedInList",e[e.e131037DisplayNameApprovalNeeded=131037]="e131037DisplayNameApprovalNeeded",e[e.e131042BusinessEligibilityPaymentIssue=131042]="e131042BusinessEligibilityPaymentIssue",e[e.e131045IncorrectCertificate=131045]="e131045IncorrectCertificate",e[e.e131047ReEngagementMessage=131047]="e131047ReEngagementMessage",e[e.e131049MetaChoseNotToDeliver=131049]="e131049MetaChoseNotToDeliver",e[e.e131050UserStoppedMarketingMessages=131050]="e131050UserStoppedMarketingMessages",e[e.e131051UnsupportedMessageType=131051]="e131051UnsupportedMessageType",e[e.e131052MediaDownloadError=131052]="e131052MediaDownloadError",e[e.e131053MediaUploadError=131053]="e131053MediaUploadError",e[e.e131057AccountInMaintenanceMode=131057]="e131057AccountInMaintenanceMode",e[e.e132000TemplateParamCountMismatch=132e3]="e132000TemplateParamCountMismatch",e[e.e132001TemplateDoesNotExist=132001]="e132001TemplateDoesNotExist",e[e.e132005TemplateHydratedTextTooLong=132005]="e132005TemplateHydratedTextTooLong",e[e.e132007TemplateFormatCharacterPolicyViolated=132007]="e132007TemplateFormatCharacterPolicyViolated",e[e.e132012TemplateParameterFormatMismatch=132012]="e132012TemplateParameterFormatMismatch",e[e.e132015TemplateIsPaused=132015]="e132015TemplateIsPaused",e[e.e132016TemplateIsDisabled=132016]="e132016TemplateIsDisabled",e[e.e132068FlowIsBlocked=132068]="e132068FlowIsBlocked",e[e.e132069FlowIsThrottled=132069]="e132069FlowIsThrottled",e[e.e133000IncompleteDeregistration=133e3]="e133000IncompleteDeregistration",e[e.e133004ServerTemporarilyUnavailable=133004]="e133004ServerTemporarilyUnavailable",e[e.e133005TwoStepVerificationPinMismatch=133005]="e133005TwoStepVerificationPinMismatch",e[e.e133006PhoneNumberReVerificationNeeded=133006]="e133006PhoneNumberReVerificationNeeded",e[e.e133008TooManyTwoStepVerificationPinGuesses=133008]="e133008TooManyTwoStepVerificationPinGuesses",e[e.e133009TwoStepVerificationPinGuessedTooFast=133009]="e133009TwoStepVerificationPinGuessedTooFast",e[e.e133010PhoneNumberNotRegistered=133010]="e133010PhoneNumberNotRegistered",e[e.e133015PhoneNumberDeletionPending=133015]="e133015PhoneNumberDeletionPending",e[e.e134011PaymentsTermsNotAccepted=134011]="e134011PaymentsTermsNotAccepted",e[e.e135000GenericUserError=135e3]="e135000GenericUserError",e[e.e2593107SynchronizationRequestLimitExceeded=2593107]="e2593107SynchronizationRequestLimitExceeded",e[e.e2593108SynchronizationRequestOutsideTimeWindow=2593108]="e2593108SynchronizationRequestOutsideTimeWindow",e[e.e4ApiTooManyCalls=4]="e4ApiTooManyCalls",e[e.e80007RateLimitIssues=80007]="e80007RateLimitIssues",e[e.e130429RateLimitHit=130429]="e130429RateLimitHit",e[e.e131048SpamRateLimitHit=131048]="e131048SpamRateLimitHit",e[e.e131056BusinessConsumerPairRateLimitHit=131056]="e131056BusinessConsumerPairRateLimitHit",e[e.e133016AccountRegisterDeregisterRateLimitExceeded=133016]="e133016AccountRegisterDeregisterRateLimitExceeded",e))(d||{}),p={};r(p,{actions:()=>D,createContactUrl:()=>k,flows:()=>A,parsers:()=>L,security:()=>O,toGraphLanguageTag:()=>R,utils:()=>K});var u={};r(u,{create:()=>h,delete:()=>b,get:()=>v,getMany:()=>T,getPreview:()=>E,publish:()=>P,updateJson:()=>g,updateMetadata:()=>w});var m={};r(m,{settings:()=>y});var f={},y={setup:e=>{Object.assign(f,e)},get:e=>{const t=f[e]??process.env[e];if(!t)throw new Error(`Missing environment variable: ${e}`);return t}};async function _(e,t,a){const r=`https://graph.facebook.com/v${y.get("GRAPH_API_VERSION")}`,n={Authorization:`Bearer ${y.get("META_APP_ACCESS_TOKEN")}`};let s=e.url;const i={...n,...e.headers,...t.headers},o=Object.fromEntries(Object.entries(t.query??{}).filter((([,e])=>null!=e))),c=new URLSearchParams(o).toString();"/"===s[0]&&(s=s.slice(1)),s=`${r}/${s}`,c&&(s=`${s}?${c}`);const l={waba_id:y.get("WHATSAPP_ACCOUNT_ID"),number_id:y.get("WHATSAPP_NUMBER_ID"),...t.params};for(const e in l)s=s.replace(`{${e}}`,l[e]);const d=t.body;let p;if(d&&a?.asFormData){const e=new FormData;for(const t in d)e.append(t,d[t]);p=e}else d&&a?.asUrlEncoded?p=new URLSearchParams(d).toString():d&&(p=JSON.stringify(d));return await fetch(s,{method:e.method,headers:i,body:p}).then((e=>e.json())).then((e=>{if("error"in e)throw{response:{data:e.error}};return e})).catch((async t=>{if(!t.response)throw new Error(t.message,{cause:{method:e.method,url:s,body:p}});const a=t.response.data.message,r=t.response.data.error_data?.details||"",n=a.match(/^\(#\d+\)/),i=n?n[0]:"",o=a.replace(/^\(#\d+\)\s*/,""),c=r?r?.startsWith?.(o)?`${i} ${r}`:`${a}: ${r}`:a;t.response.data.message=c;const{message:l,...d}=t.response.data;throw new Error(l,{cause:d})}))}async function h(e){const t=l.flows.create,a=e.flow_json?JSON.stringify(e.flow_json,null,2):void 0;return await _(t,{body:{name:e.name,categories:e.categories,clone_flow_id:e.clone_flow_id,endpoint_uri:e.endpoint_uri,publish:e.publish,flow_json:a}})}async function w(e,t){const a=l.flows.updateMetadata;return await _(a,{body:t,params:{flow_id:e}})}async function g(e,t){const a=l.flows.updateJson,r={name:"flow.json",asset_type:"FLOW_JSON",file:new Blob([JSON.stringify(t,null,2)],{type:"application/json"})};return await _(a,{params:{flow_id:e},body:r},{asFormData:!0}).catch((e=>{if(e.error_user_msg?.startsWith?.("Flow JSON has been saved"))return{success:!0,validation_errors:[]};throw e}))}async function E(e,t){const a=l.flows.getPreview,{preview:r}=await _(a,{params:{flow_id:e}}),n=r.preview_url.replaceAll("\\",""),s=Object.entries(t??{}).reduce(((e,[t,a])=>(e[t]="flow_action_payload"===t?encodeURIComponent(JSON.stringify(a)):a.toString(),e)),{});return`${n}&${new URLSearchParams(s).toString()}`}async function b(e){const t=l.flows.delete;return await _(t,{params:{flow_id:e}})}async function v(e,t){const a=l.flows.read;return await _(a,{params:{flow_id:e},query:{fields:t?.fields.join(",")??""}})}async function T(){const e=l.flows.readMany;return await _(e,{})}async function P(e){const t=l.flows.publish;return await _(t,{params:{flow_id:e}})}var S=e=>e.split("?")[0],A={createToken:({chatId:e,flow_name:t,flow_parameters:a,flow_identifier:r})=>{const n=new URLSearchParams({chat_id:e});if(n.set("flow_identifier",r??crypto.randomUUID()),a)for(const[e,t]of Object.entries(a))n.set(e,t.toString());return`${t}?${decodeURIComponent(n.toString())}`},getName:S,destructureFlowToken:e=>{const t=S(e);let a=e.split("?")?.[1];"&"===a?.[0]&&(a=a.slice(1));const r=new URLSearchParams(a),n=r.get("chat_id")||r.get("chatId"),s=r.get("flow_identifier")||r.get("flowIdentifier"),i={};for(const[e,t]of r.entries())"chat_id"!==e&&"chatId"!==e&&"flow_identifier"!==e&&"flowIdentifier"!==e&&(i[e]=t);return{paramsString:a,flowName:t,chatId:n,flowIdentifier:s,flowParameters:i}}},N=(e=>(e.AR_SA="ar-SA",e.BN_BD="bn-BD",e.BN_IN="bn-IN",e.CS_CZ="cs-CZ",e.DA_DK="da-DK",e.DE_AT="de-AT",e.DE_CH="de-CH",e.DE_DE="de-DE",e.EL_GR="el-GR",e.EN_AU="en-AU",e.EN_CA="en-CA",e.EN_GB="en-GB",e.EN_IE="en-IE",e.EN_IN="en-IN",e.EN_NZ="en-NZ",e.EN_US="en-US",e.EN_ZA="en-ZA",e.ES_AR="es-AR",e.ES_CL="es-CL",e.ES_CO="es-CO",e.ES_ES="es-ES",e.ES_MX="es-MX",e.ES_US="es-US",e.FI_FI="fi-FI",e.FR_BE="fr-BE",e.FR_CA="fr-CA",e.FR_CH="fr-CH",e.FR_FR="fr-FR",e.HE_IL="he-IL",e.HI_IN="hi-IN",e.HU_HU="hu-HU",e.ID_ID="id-ID",e.IT_CH="it-CH",e.IT_IT="it-IT",e.JA_JP="ja-JP",e.KO_KR="ko-KR",e.NL_BE="nl-BE",e.NL_NL="nl-NL",e.NO_NO="no-NO",e.PL_PL="pl-PL",e.PT_BR="pt-BR",e.PT_PT="pt-PT",e.RO_RO="ro-RO",e.RU_RU="ru-RU",e.SK_SK="sk-SK",e.SV_SE="sv-SE",e.TA_IN="ta-IN",e.TA_LK="ta-LK",e.TH_TH="th-TH",e.TR_TR="tr-TR",e.ZH_CN="zh-CN",e.ZH_HK="zh-HK",e.ZH_TW="zh-TW",e))(N||{}),I={"ar-SA":"ar","bn-BD":"bn","bn-IN":"bn","cs-CZ":"cs","da-DK":"da","de-AT":"de","de-CH":"de","de-DE":"de","el-GR":"el","en-AU":"en","en-CA":"en","en-GB":"en_GB","en-IE":"en","en-IN":"en","en-NZ":"en","en-ZA":"en","en-US":"en_US","es-AR":"es_AR","es-CL":"es","es-CO":"es","es-ES":"es_ES","es-MX":"es_MX","es-US":"es","fi-FI":"fi","fr-BE":"fr","fr-CA":"fr","fr-CH":"fr","fr-FR":"fr","he-IL":"he","hi-IN":"hi","hu-HU":"hu","id-ID":"id","it-CH":"it","it-IT":"it","ja-JP":"ja","ko-KR":"ko","nl-BE":"nl","nl-NL":"nl","no-NO":"nb","pl-PL":"pl","pt-PT":"pt_PT","pt-BR":"pt_BR","ro-RO":"ro","ru-RU":"ru","sk-SK":"sk","sv-SE":"sv","ta-IN":"ta","ta-LK":"ta","th-TH":"th","tr-TR":"tr","zh-CN":"zh_CN","zh-HK":"zh_HK","zh-TW":"zh_TW"},R=e=>{const t=I[e];return void 0===t?"en":t},C=e=>{const t={to:e.to,messaging_product:"whatsapp",recipient_type:"individual"};return e.reply&&(t.context={message_id:e.reply}),e.showUrlPreviewImage&&(t.preview_url=e.showUrlPreviewImage),t},U={flow:e=>{if("flow"!==e.message.type)throw new Error("Invalid type");const{message:t,...a}=e,{flow:r,...n}=t,s=C(a);r.mode||(r.mode=y.get("WHATSAPP_FLOWS_MODE"));const i=A.createToken({chatId:s.to,flow_name:r.name,flow_parameters:r.parameters,flow_identifier:r.identifier});return{type:"interactive",...s,interactive:{...n,type:"flow",action:{name:"flow",parameters:{flow_name:r.name,flow_token:i,flow_message_version:"3",flow_cta:r.button,flow_action:r.action??"navigate",mode:r.mode,...r.payload&&{flow_action_payload:{...r.payload,screen:r.payload.screen}}}}}}},text:e=>{if("text"!==e.message.type)throw new Error("Invalid type");const{message:{text:t},...a}=e,r={type:"text",text:{body:t},...C(a)};return e.message.previewUrl&&r.text&&(r.text.preview_url=e.message.previewUrl),r},list:e=>{if("list"!==e.message.type)throw new Error("Invalid type");const{message:t,...a}=e,{list:r,type:n,...s}=t;return{...C(a),type:"interactive",interactive:{...s,type:n,action:r}}},button:e=>{if("button"!==e.message.type)throw new Error("Invalid type");const{message:t,...a}=e,{buttons:r,type:n,...s}=t;return{...C(a),type:"interactive",interactive:{...s,type:n,action:{buttons:r.map((e=>({reply:{id:e.id,title:e.text},type:"reply"})))}}}},template:e=>{if("template"!==e.message.type)throw new Error("Invalid type");const{message:{type:t,...a},...r}=e,n={...C(r),type:t,template:{...a,language:{code:R(a.language)}}};return n.template&&(n.template.namespace=process.env.WHATSAPP_MESSAGE_NAMESPACE),n},media:e=>{if("media"!==e.message.type)throw new Error("Invalid type");const{message:{type:t,...a},...r}=e,n=Object.entries(a)[0],[s,{ref:i,...o}]=n,c=o,l=C(r);return Number.isNaN(Number(i))?c.link=i:c.id=i,{type:s,[s]:c,...l}},contact:e=>{if("contact"!==e.message.type)throw new Error("Invalid type");const{message:{contacts:t},...a}=e;return{type:"contacts",contacts:t,...C(a)}}};function B(e){const{encrypted_aes_key:a,encrypted_flow_data:r,initial_vector:n}=e,s=t.default.createPrivateKey({key:y.get("WHATSAPP_ACCOUNT_ENCRYPTION_PRIVATE_KEY"),passphrase:y.get("WHATSAPP_ACCOUNT_ENCRYPTION_PASSPHRASE")}),i=t.default.privateDecrypt({key:s,padding:t.default.constants.RSA_PKCS1_OAEP_PADDING,oaepHash:"sha256"},Buffer.from(a,"base64")),o=Buffer.from(r,"base64"),c=Buffer.from(n,"base64"),l=o.subarray(0,-16),d=o.subarray(-16),p=t.default.createDecipheriv("aes-128-gcm",i,c);p.setAuthTag(d);const u=Buffer.concat([p.update(l),p.final()]).toString("utf-8");return{payload:JSON.parse(u),encryptionMetadata:{aesKeyBuffer:i,initialVectorBuffer:c}}}function M(e){const t=new URL(e.url).searchParams,a=t.get("hub.verify_token"),r=t.get("hub.challenge");return!(!a||!r)&&a===y.get("WHATSAPP_WEBHOOK_KEY")&&r}async function x(e,a){const r=e.headers.get("x-hub-signature-256");if(!r)return!1;const n=r.replace("sha256=",""),s=t.default.createHmac("sha256",y.get("WHATSAPP_WEBHOOK_KEY")).update(a,"utf-8").digest("hex");return!!t.default.timingSafeEqual(Buffer.from(n),Buffer.from(s))}var L={toGraph:{sendMessage:e=>U[e.message.type](e),flowResponse:(e,t)=>{if(!t.data)throw new Error("Missing data in flow response");if(!t.screen)throw new Error("Missing screen in flow response");if("SUCCESS"===t.screen){const a=t.data??{};t.data={extension_message_response:{params:{flow_token:e,...a}}}}return t}},toSDK:{webhook:async function(e){if("GET"===e.method){const t=M(e);if(!t)throw new Error("Invalid health check hub");return{type:"healthCheck",payload:t}}const t=await e.text(),a=JSON.parse(t);if("entry"in a){if(!x(e,t))throw new Error("Invalid application webhook signature");const n=a.entry.flatMap((e=>e.changes)).filter((e=>"messages"===e.field)).flatMap((e=>e.value.messages)).filter(Boolean).map((e=>function(e){const t=e.from,a={id:e.id,type:e.type,timestamp:e.timestamp,metadata:{forwarded:e.context?.forwarded,frequentlyForwarded:e.context?.frequently_forwarded}},r=function(e){if(!(e.audio||e.document||e.video||e.image))return null;const t=e.audio?.id??e.document?.id??e.image?.id??e.video?.id,a=e.audio?.mime_type??e.document?.mime_type??e.image?.mime_type??e.video?.mime_type,r=["audio","document","image","video"].find((t=>e[t])),n=e.image?.sha256??e.document?.sha256??e.video?.sha256;return{id:t,type:r,caption:e.document?.caption??e.image?.caption??e.video?.caption??null,mime_type:a,sha256:n,filename:e.document?.filename??e.video?.filename}}(e),n=function(e){return e.text?.body??e.button?.text??e.interactive?.button_reply?.title??e.interactive?.list_reply?.title??e.document?.caption??null}(e),s=function(e){return e.interactive?{...e.interactive?.button_reply||e.button?{button:{id:e.interactive?.button_reply?.id??null,title:e.button?.text??e.interactive.button_reply?.title,payload:e.button?.payload??null}}:{},...e.interactive.list_reply?{selectedOption:{id:e.interactive.list_reply.id,title:e.interactive.list_reply.title,description:e.interactive.list_reply.description}}:{},...e.interactive.nfm_reply?{flowResponse:JSON.parse(e.interactive.nfm_reply.response_json)}:{}}:null}(e);return{...a,...s&&{interaction:s},...r&&{media:r},text:n,chatId:t}}(e)));return{type:"application",payload:{messageReceived:(r=n,r.length?r:void 0)}}}var r;if("encrypted_flow_data"in a)return{type:"flowExchange",payload:{...B(a),pingResponse:{data:{status:"active"}}}};throw new Error("Unrecognized event")}}},D={messages:{send:async function(e){const t=L.toGraph.sendMessage(e),a=l.messages.send;return await _(a,{body:t})},showTypingIndicator:async function(e){const t=l.messages.showTypingIndicator;return await _(t,{body:{messaging_product:"whatsapp",status:"read",typing_indicator:{type:"text"},message_id:e.messageId}})}},media:{retrieve:async function({id:e}){const t=l.media.fetch;return await _(t,{query:{phone_number_id:y.get("WHATSAPP_NUMBER_ID")},params:{media_id:e}})}},flows:{...u},waba:{registerNumber:async function({dataRegion:e,pin:t}){const a=l.waba.registerNumber,r={messaging_product:"whatsapp",pin:t};return e&&(r.data_localization_region=e),await _(a,{body:r},{asUrlEncoded:!0})},encryption:{upload:async function(e){const t=l.waba.updateEncryption,a={business_public_key:e};return await _(t,{body:a},{asUrlEncoded:!0})}}}};async function H(e){return await fetch(e).then((async e=>{const t=await e.arrayBuffer();return Buffer.from(t)}))}var O={verifyHub:M,verifySignature:x,generateWabaEncryption:async function(){const e=t.default.randomUUID(),{publicKey:a,privateKey:r}=t.default.generateKeyPairSync("rsa",{modulusLength:2048,publicKeyEncoding:{type:"spki",format:"pem"},privateKeyEncoding:{type:"pkcs8",format:"pem",cipher:"aes-256-cbc",passphrase:e}});return{passphrase:e,publicKey:a,privateKey:r}},decryptFlowBody:B,encryptFlowResponse:function(e,a){const r=[];for(const e of Buffer.from(a.initialVectorBuffer).entries())r.push(~e[1]);const n=t.default.createCipheriv("aes-128-gcm",Buffer.from(a.aesKeyBuffer),Buffer.from(r));return Buffer.concat([n.update(JSON.stringify(e),"utf-8"),n.final(),n.getAuthTag()]).toString("base64")},decryptFlowMedia:async function(e,a="https://picsum.photos/seed/picsum/200"){let{cdn_url:r,encryption_metadata:n}=e;if("EXAMPLE_DATA__CDN_URL_WILL_COME_IN_THIS_FIELD"===r)return r=a,await H(r);const s=await H(r),{iv:i,encryption_key:o,hmac_key:c,encrypted_hash:l,plaintext_hash:d}=n,p={iv:Buffer.from(i,"base64"),encryption_key:Buffer.from(o,"base64"),hmac_key:Buffer.from(c,"base64")};if(t.default.createHash("sha256").update(s).digest("base64")!==l)throw new Error("Encrypted hash validation failed");const u=s.subarray(0,s.length-10),m=s.subarray(-10);if(!t.default.createHmac("sha256",p.hmac_key).update(Buffer.concat([p.iv,u])).digest().subarray(0,10).equals(m))throw new Error("HMAC validation failed");const f=t.default.createDecipheriv("aes-256-cbc",p.encryption_key,p.iv),y=f.update(u),_=Buffer.concat([y,f.final()]);if(t.default.createHash("sha256").update(_).digest("base64")!==d)throw new Error("Decrypted media hash validation failed");return _}},k=(e,t)=>{const a=new URL("/send","https://api.whatsapp.com/"),r=e.replace(/[()\-+ ]/g,"");return a.searchParams.append("phone",r),t&&a.searchParams.append("text",t),a.href},K={flows:A,createContactUrl:k},F=null;exports.BCP47LanguageTag=N,exports.WhatsappErrorCode=d,exports.actions=D,exports.createContactUrl=k,exports.createWhatsapp=()=>F||(F=(()=>{const{settings:e}=m,{actions:t,flows:a,parsers:r,security:s,utils:i,toGraphLanguageTag:o}=p,{endpoints:c}=n;return{settings:e,sdk:{actions:t,flows:a,parsers:r,security:s,utils:i,toGraphLanguageTag:o},graph:{endpoints:c}}})()),exports.endpoints=l,exports.flows=A,exports.flowsEndpoints=s,exports.mediaEndpoints=c,exports.messagesEndpoints=i,exports.parsers=L,exports.security=O,exports.toGraphLanguageTag=R,exports.utils=K,exports.wabaEndpoints=o;
package/dist/index.d.cts CHANGED
@@ -96,6 +96,9 @@ declare const endpoints: {
96
96
  send: Endpoint<"/{number_id}/messages", "post", {
97
97
  body: WhatsappSendMessageRequestBody<any>;
98
98
  }, WhatsappSendMessageResponse>;
99
+ showTypingIndicator: Endpoint<"/{number_id}/messages", "post", {
100
+ body: WhatsappShowTypingIndicatorRequestBody;
101
+ }, WhatsappShowTypingIndicatorRequestResponse>;
99
102
  };
100
103
  waba: {
101
104
  updateEncryption: Endpoint<"/{number_id}/whatsapp_business_encryption", "post", {
@@ -105,6 +108,13 @@ declare const endpoints: {
105
108
  body: WhatsappRegisterPhoneNumberRequestBody;
106
109
  }, unknown>;
107
110
  };
111
+ media: {
112
+ fetch: Endpoint<"/{media_id}", "get", {
113
+ query: {
114
+ phone_number_id?: string;
115
+ };
116
+ }, WhatsappMediaRetrieveRequestResponse>;
117
+ };
108
118
  };
109
119
 
110
120
  type FlowCategory = "SIGN_UP" | "SIGN_IN" | "APPOINTMENT_BOOKING" | "LEAD_GENERATION" | "CONTACT_US" | "CUSTOMER_SUPPORT" | "SURVEY" | "OTHER";
@@ -138,22 +148,6 @@ interface FlowValidationError {
138
148
  column_end: number;
139
149
  }
140
150
 
141
- type WhatsappGetManyFlowsRequestResponse = {
142
- data: Array<{
143
- id: string;
144
- name: string;
145
- status: FlowStatus;
146
- categories: FlowCategory[];
147
- validation_errors: FlowValidationError[];
148
- }>;
149
- paging: {
150
- cursors: {
151
- before: string;
152
- after: string;
153
- };
154
- };
155
- };
156
-
157
151
  type WhatsappGetFlowWebPreviewPageRequestQuery = {
158
152
  flow_token: string;
159
153
  flow_action: "data_exchange" | "navigate";
@@ -192,6 +186,22 @@ type WhatsappGetFlowsRequestResponse = {
192
186
  };
193
187
  };
194
188
 
189
+ type WhatsappGetManyFlowsRequestResponse = {
190
+ data: Array<{
191
+ id: string;
192
+ name: string;
193
+ status: FlowStatus;
194
+ categories: FlowCategory[];
195
+ validation_errors: FlowValidationError[];
196
+ }>;
197
+ paging: {
198
+ cursors: {
199
+ before: string;
200
+ after: string;
201
+ };
202
+ };
203
+ };
204
+
195
205
  type WhatsappPublishFlowResponse = {
196
206
  success: boolean;
197
207
  };
@@ -235,14 +245,30 @@ declare const flowsEndpoints: {
235
245
  publish: Endpoint<"/{flow_id}/publish", "post", IRequest, WhatsappPublishFlowResponse>;
236
246
  };
237
247
 
238
- type FlowData<K extends string = string> = Record<K, AnyType>;
248
+ type WhatsappMediaRetrieveRequestResponse = {
249
+ messaging_product: "whatsapp";
250
+ url: string;
251
+ mime_type: string;
252
+ sha256: string;
253
+ file_size: number;
254
+ id: string;
255
+ };
239
256
 
240
- interface FlowMetadata {
241
- name: string;
242
- categories: FlowCategory[];
243
- application_id?: string;
244
- endpoint_uri?: string;
257
+ declare const mediaEndpoints: {
258
+ fetch: Endpoint<"/{media_id}", "get", {
259
+ query: {
260
+ phone_number_id?: string;
261
+ };
262
+ }, WhatsappMediaRetrieveRequestResponse>;
263
+ };
264
+
265
+ interface WaIncomingContactObject {
266
+ wa_id: string;
267
+ profile: {
268
+ name: string;
269
+ };
245
270
  }
271
+ type WaIncomingContacts = Array<WaIncomingContactObject>;
246
272
 
247
273
  type WaOutgoingContactsMessage = Array<{
248
274
  addresses?: Array<Partial<{
@@ -449,14 +475,6 @@ interface WaOutgoingMessage {
449
475
  }
450
476
  type WaMessageType = keyof WaOutgoingMessage;
451
477
 
452
- interface WaIncomingContactObject {
453
- wa_id: string;
454
- profile: {
455
- name: string;
456
- };
457
- }
458
- type WaIncomingContacts = Array<WaIncomingContactObject>;
459
-
460
478
  type WhatsappSendMessageRequestConfig = {
461
479
  to: string;
462
480
  preview_url?: boolean;
@@ -478,18 +496,33 @@ type WhatsappSendMessageResponse = {
478
496
  }>;
479
497
  };
480
498
 
499
+ type WhatsappShowTypingIndicatorRequestBody = {
500
+ messaging_product: "whatsapp";
501
+ status: "read";
502
+ message_id: string;
503
+ typing_indicator: {
504
+ type: "text";
505
+ };
506
+ };
507
+ type WhatsappShowTypingIndicatorRequestResponse = {
508
+ success: boolean;
509
+ };
510
+
481
511
  declare const messagesEndpoints: {
482
512
  send: Endpoint<"/{number_id}/messages", "post", {
483
513
  body: WhatsappSendMessageRequestBody<any>;
484
514
  }, WhatsappSendMessageResponse>;
515
+ showTypingIndicator: Endpoint<"/{number_id}/messages", "post", {
516
+ body: WhatsappShowTypingIndicatorRequestBody;
517
+ }, WhatsappShowTypingIndicatorRequestResponse>;
485
518
  };
486
519
 
487
- type ISOCountryCode = "AU" | "ID" | "IN" | "JP" | "SG" | "KR" | "DE" | "CH" | "GB" | "BR" | "BH" | "ZA" | "AE" | "CA";
520
+ type WhatsappISOCountryCode = "AU" | "ID" | "IN" | "JP" | "SG" | "KR" | "DE" | "CH" | "GB" | "BR" | "BH" | "ZA" | "AE" | "CA";
488
521
 
489
522
  type WhatsappRegisterPhoneNumberRequestBody = {
490
523
  messaging_product: "whatsapp";
491
524
  pin: string;
492
- data_localization_region?: ISOCountryCode;
525
+ data_localization_region?: WhatsappISOCountryCode;
493
526
  };
494
527
 
495
528
  type WhatsappWabaUploadEncryptionRequestBody = {
@@ -708,6 +741,15 @@ type WhatsappApplicationWebhookBody = {
708
741
  }>;
709
742
  };
710
743
 
744
+ type FlowData<K extends string = string> = Record<K, AnyType>;
745
+
746
+ interface FlowMetadata {
747
+ name: string;
748
+ categories: FlowCategory[];
749
+ application_id?: string;
750
+ endpoint_uri?: string;
751
+ }
752
+
711
753
  /**
712
754
  * Cloud API Error Codes
713
755
  *
@@ -1747,14 +1789,25 @@ declare function webhook(request: Request): Promise<WaSDKEvent<"healthCheck"> |
1747
1789
 
1748
1790
  declare function uploadWabaEncryption(publicKey: string): Promise<unknown>;
1749
1791
 
1750
- interface Payload {
1792
+ interface RegisterNumberPayload {
1751
1793
  pin: string;
1752
- dataRegion?: ISOCountryCode;
1794
+ dataRegion?: WhatsappISOCountryCode;
1753
1795
  }
1754
- declare function registerNumber({ dataRegion, pin }: Payload): Promise<unknown>;
1796
+ declare function registerNumber({ dataRegion, pin, }: RegisterNumberPayload): Promise<unknown>;
1755
1797
 
1756
1798
  declare function _delete(flow_id: string): Promise<WhatsappDeleteFlowResponse>;
1757
1799
 
1800
+ interface RetrieveMediaPayload {
1801
+ id: string;
1802
+ }
1803
+ declare function retrieveMedia({ id }: RetrieveMediaPayload): Promise<WhatsappMediaRetrieveRequestResponse>;
1804
+
1805
+ type WhatsappSDKShowTypingIndicatorParams = {
1806
+ messageId: string;
1807
+ };
1808
+
1809
+ declare function showTypingIndicator(params: WhatsappSDKShowTypingIndicatorParams): Promise<WhatsappShowTypingIndicatorRequestResponse>;
1810
+
1758
1811
  type WaSDKButtonMessage = Omit<WaInteractiveBase, "type"> & {
1759
1812
  type: "button";
1760
1813
  buttons: Array<{
@@ -1777,6 +1830,7 @@ interface BaseFlowConfig {
1777
1830
  button: string;
1778
1831
  parameters?: Record<string, string | number>;
1779
1832
  mode?: "draft" | "published";
1833
+ identifier?: string;
1780
1834
  }
1781
1835
  interface NavigateFlowConfig extends BaseFlowConfig {
1782
1836
  action?: "navigate";
@@ -1925,6 +1979,10 @@ declare function send(body: WaSDKSendMessageBody): Promise<WhatsappSendMessageRe
1925
1979
  declare const actions: {
1926
1980
  messages: {
1927
1981
  send: typeof send;
1982
+ showTypingIndicator: typeof showTypingIndicator;
1983
+ };
1984
+ media: {
1985
+ retrieve: typeof retrieveMedia;
1928
1986
  };
1929
1987
  flows: {
1930
1988
  create(body: Omit<WhatsappCreateFlowRequestBody, "flow_json"> & {
@@ -1990,10 +2048,11 @@ type FlowParameters = Record<string, string | number>;
1990
2048
  declare const createContactUrl: (phone: string, prefilled?: string) => string;
1991
2049
 
1992
2050
  declare const flows: {
1993
- createToken: ({ flow_name, flow_parameters, chatId, }: {
2051
+ createToken: ({ chatId, flow_name, flow_parameters, flow_identifier, }: {
2052
+ chatId: string;
1994
2053
  flow_name: string;
1995
2054
  flow_parameters?: Record<string, string | number>;
1996
- chatId: string;
2055
+ flow_identifier?: string;
1997
2056
  }) => string;
1998
2057
  getName: (token: string) => string;
1999
2058
  destructureFlowToken: (token: string) => {
@@ -2007,10 +2066,11 @@ declare const flows: {
2007
2066
 
2008
2067
  declare const utils: {
2009
2068
  flows: {
2010
- createToken: ({ flow_name, flow_parameters, chatId, }: {
2069
+ createToken: ({ chatId, flow_name, flow_parameters, flow_identifier, }: {
2070
+ chatId: string;
2011
2071
  flow_name: string;
2012
2072
  flow_parameters?: Record<string, string | number>;
2013
- chatId: string;
2073
+ flow_identifier?: string;
2014
2074
  }) => string;
2015
2075
  getName: (token: string) => string;
2016
2076
  destructureFlowToken: (token: string) => {
@@ -2048,6 +2108,10 @@ declare const createWhatsapp: () => {
2048
2108
  actions: {
2049
2109
  messages: {
2050
2110
  send: typeof send;
2111
+ showTypingIndicator: typeof showTypingIndicator;
2112
+ };
2113
+ media: {
2114
+ retrieve: typeof retrieveMedia;
2051
2115
  };
2052
2116
  flows: {
2053
2117
  create(body: Omit<WhatsappCreateFlowRequestBody, "flow_json"> & {
@@ -2074,10 +2138,11 @@ declare const createWhatsapp: () => {
2074
2138
  };
2075
2139
  };
2076
2140
  flows: {
2077
- createToken: ({ flow_name, flow_parameters, chatId, }: {
2141
+ createToken: ({ chatId, flow_name, flow_parameters, flow_identifier, }: {
2142
+ chatId: string;
2078
2143
  flow_name: string;
2079
2144
  flow_parameters?: Record<string, string | number>;
2080
- chatId: string;
2145
+ flow_identifier?: string;
2081
2146
  }) => string;
2082
2147
  getName: (token: string) => string;
2083
2148
  destructureFlowToken: (token: string) => {
@@ -2107,10 +2172,11 @@ declare const createWhatsapp: () => {
2107
2172
  };
2108
2173
  utils: {
2109
2174
  flows: {
2110
- createToken: ({ flow_name, flow_parameters, chatId, }: {
2175
+ createToken: ({ chatId, flow_name, flow_parameters, flow_identifier, }: {
2176
+ chatId: string;
2111
2177
  flow_name: string;
2112
2178
  flow_parameters?: Record<string, string | number>;
2113
- chatId: string;
2179
+ flow_identifier?: string;
2114
2180
  }) => string;
2115
2181
  getName: (token: string) => string;
2116
2182
  destructureFlowToken: (token: string) => {
@@ -2149,6 +2215,9 @@ declare const createWhatsapp: () => {
2149
2215
  send: Endpoint<"/{number_id}/messages", "post", {
2150
2216
  body: WhatsappSendMessageRequestBody<any>;
2151
2217
  }, WhatsappSendMessageResponse>;
2218
+ showTypingIndicator: Endpoint<"/{number_id}/messages", "post", {
2219
+ body: WhatsappShowTypingIndicatorRequestBody;
2220
+ }, WhatsappShowTypingIndicatorRequestResponse>;
2152
2221
  };
2153
2222
  waba: {
2154
2223
  updateEncryption: Endpoint<"/{number_id}/whatsapp_business_encryption", "post", {
@@ -2158,8 +2227,15 @@ declare const createWhatsapp: () => {
2158
2227
  body: WhatsappRegisterPhoneNumberRequestBody;
2159
2228
  }, unknown>;
2160
2229
  };
2230
+ media: {
2231
+ fetch: Endpoint<"/{media_id}", "get", {
2232
+ query: {
2233
+ phone_number_id?: string;
2234
+ };
2235
+ }, WhatsappMediaRetrieveRequestResponse>;
2236
+ };
2161
2237
  };
2162
2238
  };
2163
2239
  };
2164
2240
 
2165
- export { BCP47LanguageTag, type BaseMessageReceivedEventPayload, type ConversationType, type DataExchangeFlowConfig, type FlowAction, type FlowCanSendMessageStatus, type FlowCategory, type FlowConfig, type FlowData, type FlowMediaData, type FlowMetadata, type FlowParameters, type FlowScreen, type FlowStatus, type FlowUpdateEventPayload, type FlowValidationError, type ISOCountryCode, type MediaTypes, type MessageField, type MessageReceivedEvent, type MessageReceivedEventPayload, type MessageStatusUpdateEvent, type MetadataObject, type NavigateFlowConfig, type Payload, type WaIncomingAudioObject, type WaIncomingButtonObject, type WaIncomingContactObject, type WaIncomingContacts, type WaIncomingContextObject, type WaIncomingDocumentObject, type WaIncomingErrorObject, type WaIncomingErrors, type WaIncomingIdentityObject, type WaIncomingImageObject, type WaIncomingInteractiveObject, type WaIncomingMessageStatus, type WaIncomingMessageStatuses, type WaIncomingMessageType, type WaIncomingMessages, type WaIncomingOrderObject, type WaIncomingReferralObject, type WaIncomingStickerObject, type WaIncomingSystemObject, type WaIncomingTextObject, type WaIncomingVideoObject, type WaInteractiveAction, type WaInteractiveActionType, type WaInteractiveBase, type WaInteractiveBody, type WaInteractiveFooter, type WaInteractiveHeader, type WaMessageType, type WaOutgoingAudioMessage, type WaOutgoingContactsMessage, type WaOutgoingDocumentMessage, type WaOutgoingImageMessage, type WaOutgoingInteractiveMessage, type WaOutgoingLocationMessage, type WaOutgoingMessage, type WaOutgoingReactionMessage, type WaOutgoingStickerMessage, type WaOutgoingTemplateMessage, type WaOutgoingTextMessage, type WaOutgoingVideoMessage, type WaSDKButtonMessage, type WaSDKEvent, type WaSDKEventPayload, type WaSDKEventType, type WaSDKFlowDecryptedWebhookBody, type WaSDKFlowExchangeEventPayload, type WaSDKFlowPayload, type WaSDKGetFlowWebPreviewPageRequestQuery, type WaSDKOutgoingContactMessage, type WaSDKOutgoingFlowMessage, type WaSDKOutgoingListMessage, type WaSDKOutgoingMediaMessage, type WaSDKOutgoingTemplateMessage, type WaSDKOutgoingTextMessage, type WaSDKSendMessageBody, type WaSDKSendMessageConfig, type WaSDKSendMessageData, type WebhookSignatureChallengeArguments, type WhatsappApplicationWebhookBody, type WhatsappCreateFlowRequestBody, type WhatsappCreateFlowResponse, type WhatsappDeleteFlowResponse, WhatsappErrorCode, type WhatsappFlowDecryptedWebhookBody, type WhatsappFlowEncryptedWebhookBody, type WhatsappFlowEncryptionData, type WhatsappFlowErrorMessages, type WhatsappFlowInfo, type WhatsappFlowPingResponse, type WhatsappFlowResponse, type WhatsappFlowUpdateMetadataRequestBody, type WhatsappFlowUpdateMetadataResponse, type WhatsappGetFlowWebPreviewPageRequestQuery, type WhatsappGetFlowWebPreviewURLResponse, type WhatsappGetFlowsRequestQuery, type WhatsappGetFlowsRequestResponse, type WhatsappGetManyFlowsRequestResponse, type WhatsappLanguageTag, type WhatsappPublishFlowResponse, type WhatsappRegisterPhoneNumberRequestBody, type WhatsappSendMessageRequestBody, type WhatsappSendMessageRequestConfig, type WhatsappSendMessageResponse, type WhatsappUpdateFlowJsonRequestBody, type WhatsappUpdateFlowJsonResponse, type WhatsappWabaUploadEncryptionRequestBody, actions, createContactUrl, createWhatsapp, endpoints, flows, flowsEndpoints, messagesEndpoints, parsers, security, toGraphLanguageTag, utils, wabaEndpoints };
2241
+ export { BCP47LanguageTag, type BaseMessageReceivedEventPayload, type ConversationType, type DataExchangeFlowConfig, type FlowAction, type FlowCanSendMessageStatus, type FlowCategory, type FlowConfig, type FlowData, type FlowMediaData, type FlowMetadata, type FlowParameters, type FlowScreen, type FlowStatus, type FlowUpdateEventPayload, type FlowValidationError, type MediaTypes, type MessageField, type MessageReceivedEvent, type MessageReceivedEventPayload, type MessageStatusUpdateEvent, type MetadataObject, type NavigateFlowConfig, type RegisterNumberPayload, type RetrieveMediaPayload, type WaIncomingAudioObject, type WaIncomingButtonObject, type WaIncomingContactObject, type WaIncomingContacts, type WaIncomingContextObject, type WaIncomingDocumentObject, type WaIncomingErrorObject, type WaIncomingErrors, type WaIncomingIdentityObject, type WaIncomingImageObject, type WaIncomingInteractiveObject, type WaIncomingMessageStatus, type WaIncomingMessageStatuses, type WaIncomingMessageType, type WaIncomingMessages, type WaIncomingOrderObject, type WaIncomingReferralObject, type WaIncomingStickerObject, type WaIncomingSystemObject, type WaIncomingTextObject, type WaIncomingVideoObject, type WaInteractiveAction, type WaInteractiveActionType, type WaInteractiveBase, type WaInteractiveBody, type WaInteractiveFooter, type WaInteractiveHeader, type WaMessageType, type WaOutgoingAudioMessage, type WaOutgoingContactsMessage, type WaOutgoingDocumentMessage, type WaOutgoingImageMessage, type WaOutgoingInteractiveMessage, type WaOutgoingLocationMessage, type WaOutgoingMessage, type WaOutgoingReactionMessage, type WaOutgoingStickerMessage, type WaOutgoingTemplateMessage, type WaOutgoingTextMessage, type WaOutgoingVideoMessage, type WaSDKButtonMessage, type WaSDKEvent, type WaSDKEventPayload, type WaSDKEventType, type WaSDKFlowDecryptedWebhookBody, type WaSDKFlowExchangeEventPayload, type WaSDKFlowPayload, type WaSDKGetFlowWebPreviewPageRequestQuery, type WaSDKOutgoingContactMessage, type WaSDKOutgoingFlowMessage, type WaSDKOutgoingListMessage, type WaSDKOutgoingMediaMessage, type WaSDKOutgoingTemplateMessage, type WaSDKOutgoingTextMessage, type WaSDKSendMessageBody, type WaSDKSendMessageConfig, type WaSDKSendMessageData, type WebhookSignatureChallengeArguments, type WhatsappApplicationWebhookBody, type WhatsappCreateFlowRequestBody, type WhatsappCreateFlowResponse, type WhatsappDeleteFlowResponse, WhatsappErrorCode, type WhatsappFlowDecryptedWebhookBody, type WhatsappFlowEncryptedWebhookBody, type WhatsappFlowEncryptionData, type WhatsappFlowErrorMessages, type WhatsappFlowInfo, type WhatsappFlowPingResponse, type WhatsappFlowResponse, type WhatsappFlowUpdateMetadataRequestBody, type WhatsappFlowUpdateMetadataResponse, type WhatsappGetFlowWebPreviewPageRequestQuery, type WhatsappGetFlowWebPreviewURLResponse, type WhatsappGetFlowsRequestQuery, type WhatsappGetFlowsRequestResponse, type WhatsappGetManyFlowsRequestResponse, type WhatsappISOCountryCode, type WhatsappLanguageTag, type WhatsappMediaRetrieveRequestResponse, type WhatsappPublishFlowResponse, type WhatsappRegisterPhoneNumberRequestBody, type WhatsappSDKShowTypingIndicatorParams, type WhatsappSendMessageRequestBody, type WhatsappSendMessageRequestConfig, type WhatsappSendMessageResponse, type WhatsappShowTypingIndicatorRequestBody, type WhatsappShowTypingIndicatorRequestResponse, type WhatsappUpdateFlowJsonRequestBody, type WhatsappUpdateFlowJsonResponse, type WhatsappWabaUploadEncryptionRequestBody, actions, createContactUrl, createWhatsapp, endpoints, flows, flowsEndpoints, mediaEndpoints, messagesEndpoints, parsers, security, toGraphLanguageTag, utils, wabaEndpoints };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.0.15",
7
+ "version": "0.0.17",
8
8
  "type": "module",
9
9
  "files": [
10
10
  "dist"