@apostlejs/whatsapp 0.0.8 → 0.0.9
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 +1 -1
- package/dist/index.d.cts +16 -86
- package/package.json +4 -7
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("zod");function t(e){return e&&e.__esModule?e:{default:e}}var a=t(require("node:crypto")),r=Object.defineProperty,n=(e,t)=>{for(var a in t)r(e,a,{get:t[a],enumerable:!0})},o={};n(o,{WhatsappErrorCode:()=>h,endpoints:()=>c,flowAction:()=>d,flowCanSendMessageStatus:()=>g,flowCategory:()=>p,flowMediaData:()=>u,flowMetadata:()=>f,flowScreen:()=>m,flowStatus:()=>y,flowValidationError:()=>w,flowsEndpoints:()=>i,messagesEndpoints:()=>s,wabaEndpoints:()=>l});var i={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}},l={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:i,messages:s,waba:l},d=e.z.enum(["INIT","BACK","data_exchange","navigate","ping"]),p=e.z.enum(["SIGN_UP","SIGN_IN","APPOINTMENT_BOOKING","LEAD_GENERATION","CONTACT_US","CUSTOMER_SUPPORT","SURVEY","OTHER"]),u=e.z.object({media_id:e.z.string(),cdn_url:e.z.string(),file_name:e.z.string(),encryption_metadata:e.z.object({encrypted_hash:e.z.string(),iv:e.z.string(),encryption_key:e.z.string(),hmac_key:e.z.string(),plaintext_hash:e.z.string()})}),f=e.z.object({name:e.z.string(),categories:e.z.array(p),application_id:e.z.string().optional(),endpoint_uri:e.z.string().optional()}),m=e.z.union([e.z.literal("SUCCESS"),e.z.string()]),y=e.z.enum(["DRAFT","PUBLISHED","DEPRECATED","BLOCKED","THROTTLED"]),g=e.z.enum(["AVAILABLE","LIMITED","BLOCKED"]),w=e.z.object({error:e.z.string(),error_type:e.z.string(),message:e.z.string(),line_start:e.z.number(),line_end:e.z.number(),column_start:e.z.number(),column_end:e.z.number()}),h=(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))(h||{}),_={};n(_,{actions:()=>F,flows:()=>B,parsers:()=>V,security:()=>j,toGraphLanguageTag:()=>O,utils:()=>G});var b={};n(b,{create:()=>P,delete:()=>R,get:()=>C,getMany:()=>M,getPreview:()=>I,publish:()=>x,updateJson:()=>N,updateMetadata:()=>A});var v={};n(v,{settings:()=>S});var E={},S={setup:e=>{Object.assign(E,e)},get:e=>{const t=E[e]??process.env[e];if(!t)throw new Error(`Missing environment variable: ${e}`);return t}};async function T(e,t,a){const r=`https://graph.facebook.com/v${S.get("GRAPH_API_VERSION")}`,n={Authorization:`Bearer ${S.get("META_APP_ACCESS_TOKEN")}`};let o=e.url;const i={...n,...e.headers,...t.headers},s=new URLSearchParams(t.query).toString();"/"===o[0]&&(o=o.slice(1)),o=`${r}/${o}`,s&&(o=`${o}?${s}`);const l={waba_id:S.get("WHATSAPP_ACCOUNT_ID"),number_id:S.get("WHATSAPP_NUMBER_ID"),...t.params};for(const e in l)o=o.replace(`{${e}}`,l[e]);const c=t.body;let d;if(c&&a?.asFormData){const e=new FormData;for(const t in c)e.append(t,c[t]);d=e}else c&&a?.asUrlEncoded?d=new URLSearchParams(c).toString():c&&(d=JSON.stringify(c));return await fetch(o,{method:e.method,headers:i,body:d}).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*/,""),i=a?a.startsWith(o)?`${n} ${a}`:`${t}: ${a}`:t;e.response.data.message=i;const{message:s,...l}=e.response.data;throw new Error(s,{cause:l})}))}async function P(e){const t=c.flows.create,a=e.flow_json?JSON.stringify(e.flow_json,null,2):void 0;return await T(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 A(e,t){const a=c.flows.updateMetadata;return await T(a,{body:t,params:{flow_id:e}})}async function N(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 T(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 I(e,t){const a=c.flows.getPreview,{preview:r}=await T(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 R(e){const t=c.flows.delete;return await T(t,{params:{flow_id:e}})}async function C(e,t){const a=c.flows.read;return await T(a,{params:{flow_id:e},query:{fields:t?.fields.join(",")??""}})}async function M(){const e=c.flows.readMany;return await T(e,{})}async function x(e){const t=c.flows.publish;return await T(t,{params:{flow_id:e}})}var U=e=>e.split("?")[0],B={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:U,destructureFlowToken:e=>{const t=U(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"),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:o,flowParameters:i}}},D={"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"},O=e=>{const t=D[e];return void 0===t?"en":t},L=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},z={flow:e=>{if("flow"!==e.message.type)throw new Error("Invalid type");const{message:t,...a}=e,{flow:r,...n}=t,o=L(a);r.mode||(r.mode=S.get("WHATSAPP_FLOWS_MODE"));const i=B.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: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.toUpperCase()}}}}}}},text:e=>{if("text"!==e.message.type)throw new Error("Invalid type");const{message:{text:t},...a}=e,r={type:"text",text:{body:t},...L(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{...L(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{...L(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={...L(r),type:t,template:{...a,language:{code:O(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:i,...s}]=n,l=s,c=L(r);return Number.isNaN(Number(i))?l.link=i:l.id=i,{type:o,[o]:l,...c}},contact:e=>{if("contact"!==e.message.type)throw new Error("Invalid type");const{message:{contacts:t},...a}=e;return{type:"contacts",contacts:t,...L(a)}}};function k(e){const{encrypted_aes_key:t,encrypted_flow_data:r,initial_vector:n}=e,o=a.default.createPrivateKey({key:S.get("WHATSAPP_ACCOUNT_ENCRYPTION_PRIVATE_KEY"),passphrase:S.get("WHATSAPP_ACCOUNT_ENCRYPTION_PASSPHRASE")}),i=a.default.privateDecrypt({key:o,padding:a.default.constants.RSA_PKCS1_OAEP_PADDING,oaepHash:"sha256"},Buffer.from(t,"base64")),s=Buffer.from(r,"base64"),l=Buffer.from(n,"base64"),c=s.subarray(0,-16),d=s.subarray(-16),p=a.default.createDecipheriv("aes-128-gcm",i,l);p.setAuthTag(d);const u=Buffer.concat([p.update(c),p.final()]).toString("utf-8");return{payload:JSON.parse(u),encryptionMetadata:{aesKeyBuffer:i,initialVectorBuffer:l}}}function H(e){const t=new URL(e.url).searchParams,a=t.get("hub.verify_token"),r=t.get("hub.challenge");return!(!a||!r)&&a===S.get("WHATSAPP_WEBHOOK_KEY")&&r}async function K(e,t){const r=e.headers.get("x-hub-signature-256");if(!r)return!1;const n=r.replace("sha256=",""),o=a.default.createHmac("sha256",S.get("WHATSAPP_WEBHOOK_KEY")).update(t,"utf-8").digest("hex");return!!a.default.timingSafeEqual(Buffer.from(n),Buffer.from(o))}var V={toGraph:{sendMessage:e=>z[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(t.screen=t.screen.toUpperCase(),"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=H(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(!K(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:{...k(a),pingResponse:{data:{status:"active"}}}};throw new Error("Unrecognized event")}}},F={messages:{send:async function(e){const t=V.toGraph.sendMessage(e),a=c.messages.send;return await T(a,{body:t})}},flows:{...b},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 T(a,{body:r},{asUrlEncoded:!0})},encryption:{upload:async function(e){const t=c.waba.updateEncryption,a={business_public_key:e};return await T(t,{body:a},{asUrlEncoded:!0})}}}};async function W(e){return await fetch(e).then((async e=>{const t=await e.arrayBuffer();return Buffer.from(t)}))}var j={verifyHub:H,verifySignature:K,generateWabaEncryption:async function(){const e=a.default.randomUUID(),{publicKey:t,privateKey:r}=a.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:t,privateKey:r}},decryptFlowBody:k,encryptFlowResponse:function(e,t){const r=[];for(const e of Buffer.from(t.initialVectorBuffer).entries())r.push(~e[1]);const n=a.default.createCipheriv("aes-128-gcm",Buffer.from(t.aesKeyBuffer),Buffer.from(r));return Buffer.concat([n.update(JSON.stringify(e),"utf-8"),n.final(),n.getAuthTag()]).toString("base64")},decryptFlowMedia:async function(e){const t=[];for(const r of e){let{cdn_url:e,encryption_metadata:n}=r;if("EXAMPLE_DATA__CDN_URL_WILL_COME_IN_THIS_FIELD"===e){e="https://picsum.photos/seed/picsum/200";const a=await W(e);t.push(a);continue}const o=await W(e),{iv:i,encryption_key:s,hmac_key:l,encrypted_hash:c,plaintext_hash:d}=n,p={iv:Buffer.from(i,"base64"),encryption_key:Buffer.from(s,"base64"),hmac_key:Buffer.from(l,"base64")};if(a.default.createHash("sha256").update(o).digest("base64")!==c)throw new Error("Encrypted hash validation failed");const u=o.subarray(0,o.length-10),f=o.subarray(-10);if(!a.default.createHmac("sha256",p.hmac_key).update(Buffer.concat([p.iv,u])).digest().subarray(0,10).equals(f))throw new Error("HMAC validation failed");const m=a.default.createDecipheriv("aes-256-cbc",p.encryption_key,p.iv),y=m.update(u),g=Buffer.concat([y,m.final()]);if(a.default.createHash("sha256").update(g).digest("base64")!==d)throw new Error("Decrypted media hash validation failed");t.push(g)}return t}},G={flows:B},q=null;exports.WhatsappErrorCode=h,exports.actions=F,exports.createWhatsapp=()=>q||(q=(()=>{const{settings:e}=v,{actions:t,flows:a,parsers:r,security:n,utils:i,toGraphLanguageTag:s}=_,{endpoints:l}=o;return{settings:e,sdk:{actions:t,flows:a,parsers:r,security:n,utils:i,toGraphLanguageTag:s},graph:{endpoints:l}}})()),exports.endpoints=c,exports.flowAction=d,exports.flowCanSendMessageStatus=g,exports.flowCategory=p,exports.flowMediaData=u,exports.flowMetadata=f,exports.flowScreen=m,exports.flowStatus=y,exports.flowValidationError=w,exports.flows=B,exports.flowsEndpoints=i,exports.messagesEndpoints=s,exports.parsers=V,exports.security=j,exports.toGraphLanguageTag=O,exports.utils=G,exports.wabaEndpoints=l;
|
|
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:()=>i,wabaEndpoints:()=>s});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}},i={send:{url:"/{number_id}/messages",method:"post",headers:{"Content-Type":"application/json"},request:{body:null},response:null}},s={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:i,waba:s},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||{}),d={};r(d,{actions:()=>B,flows:()=>S,parsers:()=>U,security:()=>k,toGraphLanguageTag:()=>N,utils:()=>L});var p={};r(p,{create:()=>h,delete:()=>b,get:()=>v,getMany:()=>P,getPreview:()=>_,publish:()=>E,updateJson:()=>g,updateMetadata:()=>w});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 i={...n,...e.headers,...t.headers},s=new URLSearchParams(t.query).toString();"/"===o[0]&&(o=o.slice(1)),o=`${r}/${o}`,s&&(o=`${o}?${s}`);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 d;if(l&&a?.asFormData){const e=new FormData;for(const t in l)e.append(t,l[t]);d=e}else l&&a?.asUrlEncoded?d=new URLSearchParams(l).toString():l&&(d=JSON.stringify(l));return await fetch(o,{method:e.method,headers:i,body:d}).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*/,""),i=a?a.startsWith(o)?`${n} ${a}`:`${t}: ${a}`:t;e.response.data.message=i;const{message:s,...c}=e.response.data;throw new Error(s,{cause:c})}))}async function h(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 w(e,t){const a=c.flows.updateMetadata;return await y(a,{body:t,params:{flow_id:e}})}async function g(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 _(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 b(e){const t=c.flows.delete;return await y(t,{params:{flow_id:e}})}async function v(e,t){const a=c.flows.read;return await y(a,{params:{flow_id:e},query:{fields:t?.fields.join(",")??""}})}async function P(){const e=c.flows.readMany;return await y(e,{})}async function E(e){const t=c.flows.publish;return await y(t,{params:{flow_id:e}})}var T=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:T,destructureFlowToken:e=>{const t=T(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"),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:o,flowParameters:i}}},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"},N=e=>{const t=A[e];return void 0===t?"en":t},I=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},R={flow:e=>{if("flow"!==e.message.type)throw new Error("Invalid type");const{message:t,...a}=e,{flow:r,...n}=t,o=I(a);r.mode||(r.mode=m.get("WHATSAPP_FLOWS_MODE"));const i=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: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.toUpperCase()}}}}}}},text:e=>{if("text"!==e.message.type)throw new Error("Invalid type");const{message:{text:t},...a}=e,r={type:"text",text:{body:t},...I(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{...I(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{...I(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={...I(r),type:t,template:{...a,language:{code:N(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:i,...s}]=n,c=s,l=I(r);return Number.isNaN(Number(i))?c.link=i:c.id=i,{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,...I(a)}}};function M(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")}),i=t.default.privateDecrypt({key:o,padding:t.default.constants.RSA_PKCS1_OAEP_PADDING,oaepHash:"sha256"},Buffer.from(a,"base64")),s=Buffer.from(r,"base64"),c=Buffer.from(n,"base64"),l=s.subarray(0,-16),d=s.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 C(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 x(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 U={toGraph:{sendMessage:e=>R[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(t.screen=t.screen.toUpperCase(),"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=C(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),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:{...M(a),pingResponse:{data:{status:"active"}}}};throw new Error("Unrecognized event")}}},B={messages:{send:async function(e){const t=U.toGraph.sendMessage(e),a=c.messages.send;return await y(a,{body:t})}},flows:{...p},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 k={verifyHub:C,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:M,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){const a=[];for(const r of e){let{cdn_url:e,encryption_metadata:n}=r;if("EXAMPLE_DATA__CDN_URL_WILL_COME_IN_THIS_FIELD"===e){e="https://picsum.photos/seed/picsum/200";const t=await D(e);a.push(t);continue}const o=await D(e),{iv:i,encryption_key:s,hmac_key:c,encrypted_hash:l,plaintext_hash:d}=n,p={iv:Buffer.from(i,"base64"),encryption_key:Buffer.from(s,"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",p.hmac_key).update(Buffer.concat([p.iv,u])).digest().subarray(0,10).equals(f))throw new Error("HMAC validation failed");const m=t.default.createDecipheriv("aes-256-cbc",p.encryption_key,p.iv),y=m.update(u),h=Buffer.concat([y,m.final()]);if(t.default.createHash("sha256").update(h).digest("base64")!==d)throw new Error("Decrypted media hash validation failed");a.push(h)}return a}},L={flows:S},O=null;exports.WhatsappErrorCode=l,exports.actions=B,exports.createWhatsapp=()=>O||(O=(()=>{const{settings:e}=u,{actions:t,flows:a,parsers:r,security:o,utils:i,toGraphLanguageTag:s}=d,{endpoints:c}=n;return{settings:e,sdk:{actions:t,flows:a,parsers:r,security:o,utils:i,toGraphLanguageTag:s},graph:{endpoints:c}}})()),exports.endpoints=c,exports.flows=S,exports.flowsEndpoints=o,exports.messagesEndpoints=i,exports.parsers=U,exports.security=k,exports.toGraphLanguageTag=N,exports.utils=L,exports.wabaEndpoints=s;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
|
|
3
1
|
type AnyType = any;
|
|
4
2
|
type Defined<T> = Exclude<T, undefined>;
|
|
5
3
|
|
|
@@ -78,41 +76,7 @@ declare enum LanguageTag {
|
|
|
78
76
|
ZH_TW = "zh-TW"
|
|
79
77
|
}
|
|
80
78
|
|
|
81
|
-
|
|
82
|
-
media_id: z.ZodString;
|
|
83
|
-
cdn_url: z.ZodString;
|
|
84
|
-
file_name: z.ZodString;
|
|
85
|
-
encryption_metadata: z.ZodObject<{
|
|
86
|
-
encrypted_hash: z.ZodString;
|
|
87
|
-
iv: z.ZodString;
|
|
88
|
-
encryption_key: z.ZodString;
|
|
89
|
-
hmac_key: z.ZodString;
|
|
90
|
-
plaintext_hash: z.ZodString;
|
|
91
|
-
}, "strip", z.ZodTypeAny, {
|
|
92
|
-
encrypted_hash: string;
|
|
93
|
-
iv: string;
|
|
94
|
-
encryption_key: string;
|
|
95
|
-
hmac_key: string;
|
|
96
|
-
plaintext_hash: string;
|
|
97
|
-
}, {
|
|
98
|
-
encrypted_hash: string;
|
|
99
|
-
iv: string;
|
|
100
|
-
encryption_key: string;
|
|
101
|
-
hmac_key: string;
|
|
102
|
-
plaintext_hash: string;
|
|
103
|
-
}>;
|
|
104
|
-
}, "strip", z.ZodTypeAny, {
|
|
105
|
-
media_id: string;
|
|
106
|
-
cdn_url: string;
|
|
107
|
-
file_name: string;
|
|
108
|
-
encryption_metadata: {
|
|
109
|
-
encrypted_hash: string;
|
|
110
|
-
iv: string;
|
|
111
|
-
encryption_key: string;
|
|
112
|
-
hmac_key: string;
|
|
113
|
-
plaintext_hash: string;
|
|
114
|
-
};
|
|
115
|
-
}, {
|
|
79
|
+
interface FlowMediaData {
|
|
116
80
|
media_id: string;
|
|
117
81
|
cdn_url: string;
|
|
118
82
|
file_name: string;
|
|
@@ -123,8 +87,7 @@ declare const flowMediaData: z.ZodObject<{
|
|
|
123
87
|
hmac_key: string;
|
|
124
88
|
plaintext_hash: string;
|
|
125
89
|
};
|
|
126
|
-
}
|
|
127
|
-
type FlowMediaData = z.infer<typeof flowMediaData>;
|
|
90
|
+
}
|
|
128
91
|
|
|
129
92
|
declare function decryptFlowMedia(media: FlowMediaData[]): Promise<Buffer<ArrayBufferLike>[]>;
|
|
130
93
|
|
|
@@ -133,11 +96,9 @@ declare function encryptFlowResponse(data: AnyType, encryptionMetadata: {
|
|
|
133
96
|
initialVectorBuffer: Buffer;
|
|
134
97
|
}): string;
|
|
135
98
|
|
|
136
|
-
|
|
137
|
-
type FlowAction = z.infer<typeof flowAction>;
|
|
99
|
+
type FlowAction = "INIT" | "BACK" | "data_exchange" | "navigate" | "ping";
|
|
138
100
|
|
|
139
|
-
|
|
140
|
-
type FlowScreen = z.infer<typeof flowScreen>;
|
|
101
|
+
type FlowScreen = "SUCCESS" | (string & {});
|
|
141
102
|
|
|
142
103
|
interface WhatsappFlowDecryptedWebhookBody {
|
|
143
104
|
version: "3.0";
|
|
@@ -205,8 +166,7 @@ declare const endpoints: {
|
|
|
205
166
|
};
|
|
206
167
|
};
|
|
207
168
|
|
|
208
|
-
|
|
209
|
-
type FlowCategory = z.infer<typeof flowCategory>;
|
|
169
|
+
type FlowCategory = "SIGN_UP" | "SIGN_IN" | "APPOINTMENT_BOOKING" | "LEAD_GENERATION" | "CONTACT_US" | "CUSTOMER_SUPPORT" | "SURVEY" | "OTHER";
|
|
210
170
|
|
|
211
171
|
type WhatsappCreateFlowRequestBody = {
|
|
212
172
|
name: string;
|
|
@@ -224,37 +184,18 @@ type WhatsappDeleteFlowResponse = {
|
|
|
224
184
|
success: boolean;
|
|
225
185
|
};
|
|
226
186
|
|
|
227
|
-
|
|
228
|
-
type
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
declare const flowValidationError: z.ZodObject<{
|
|
233
|
-
error: z.ZodString;
|
|
234
|
-
error_type: z.ZodString;
|
|
235
|
-
message: z.ZodString;
|
|
236
|
-
line_start: z.ZodNumber;
|
|
237
|
-
line_end: z.ZodNumber;
|
|
238
|
-
column_start: z.ZodNumber;
|
|
239
|
-
column_end: z.ZodNumber;
|
|
240
|
-
}, "strip", z.ZodTypeAny, {
|
|
241
|
-
message: string;
|
|
187
|
+
type FlowStatus = "DRAFT" | "PUBLISHED" | "DEPRECATED" | "BLOCKED" | "THROTTLED";
|
|
188
|
+
type FlowCanSendMessageStatus = "AVAILABLE" | "LIMITED" | "BLOCKED";
|
|
189
|
+
|
|
190
|
+
interface FlowValidationError {
|
|
242
191
|
error: string;
|
|
243
192
|
error_type: string;
|
|
244
|
-
line_start: number;
|
|
245
|
-
line_end: number;
|
|
246
|
-
column_start: number;
|
|
247
|
-
column_end: number;
|
|
248
|
-
}, {
|
|
249
193
|
message: string;
|
|
250
|
-
error: string;
|
|
251
|
-
error_type: string;
|
|
252
194
|
line_start: number;
|
|
253
195
|
line_end: number;
|
|
254
196
|
column_start: number;
|
|
255
197
|
column_end: number;
|
|
256
|
-
}
|
|
257
|
-
type FlowValidationError = z.infer<typeof flowValidationError>;
|
|
198
|
+
}
|
|
258
199
|
|
|
259
200
|
type WhatsappGetManyFlowsRequestResponse = {
|
|
260
201
|
data: Array<{
|
|
@@ -355,23 +296,12 @@ declare const flowsEndpoints: {
|
|
|
355
296
|
|
|
356
297
|
type FlowData<K extends string = string> = Record<K, AnyType>;
|
|
357
298
|
|
|
358
|
-
|
|
359
|
-
name: z.ZodString;
|
|
360
|
-
categories: z.ZodArray<z.ZodEnum<["SIGN_UP", "SIGN_IN", "APPOINTMENT_BOOKING", "LEAD_GENERATION", "CONTACT_US", "CUSTOMER_SUPPORT", "SURVEY", "OTHER"]>, "many">;
|
|
361
|
-
application_id: z.ZodOptional<z.ZodString>;
|
|
362
|
-
endpoint_uri: z.ZodOptional<z.ZodString>;
|
|
363
|
-
}, "strip", z.ZodTypeAny, {
|
|
364
|
-
name: string;
|
|
365
|
-
categories: ("SIGN_UP" | "SIGN_IN" | "APPOINTMENT_BOOKING" | "LEAD_GENERATION" | "CONTACT_US" | "CUSTOMER_SUPPORT" | "SURVEY" | "OTHER")[];
|
|
366
|
-
application_id?: string | undefined;
|
|
367
|
-
endpoint_uri?: string | undefined;
|
|
368
|
-
}, {
|
|
299
|
+
interface FlowMetadata {
|
|
369
300
|
name: string;
|
|
370
|
-
categories:
|
|
371
|
-
application_id?: string
|
|
372
|
-
endpoint_uri?: string
|
|
373
|
-
}
|
|
374
|
-
type FlowMetadata = z.infer<typeof flowMetadata>;
|
|
301
|
+
categories: FlowCategory[];
|
|
302
|
+
application_id?: string;
|
|
303
|
+
endpoint_uri?: string;
|
|
304
|
+
}
|
|
375
305
|
|
|
376
306
|
type WaOutgoingContactsMessage = Array<{
|
|
377
307
|
addresses?: Array<Partial<{
|
|
@@ -2221,4 +2151,4 @@ declare const createWhatsapp: () => {
|
|
|
2221
2151
|
};
|
|
2222
2152
|
};
|
|
2223
2153
|
|
|
2224
|
-
export { 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, createWhatsapp, endpoints,
|
|
2154
|
+
export { 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, createWhatsapp, endpoints, flows, flowsEndpoints, 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.
|
|
7
|
+
"version": "0.0.9",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist"
|
|
@@ -33,14 +33,11 @@
|
|
|
33
33
|
"vitest": "^3.2.4",
|
|
34
34
|
"@apostlejs/config": "1.0.0"
|
|
35
35
|
},
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"zod": "3.24.2"
|
|
38
|
-
},
|
|
39
36
|
"scripts": {
|
|
40
37
|
"------------- toolchain -------------": "-------------",
|
|
41
38
|
"format": "biome format --write",
|
|
42
39
|
"lint": "biome lint --error-on-warnings",
|
|
43
|
-
"
|
|
40
|
+
"checktypes": "tsc --noEmit",
|
|
44
41
|
"test": "vitest --disable-console-intercept",
|
|
45
42
|
"------------- dev -------------": "-------------",
|
|
46
43
|
"dev:barrelsmith": "barrelsmith --watch",
|
|
@@ -53,7 +50,7 @@
|
|
|
53
50
|
"------------- publishing -------------": "-------------",
|
|
54
51
|
"change": "changeset",
|
|
55
52
|
"bump": "pnpm changeset version",
|
|
56
|
-
"release": "pnpm
|
|
57
|
-
"release:rc": "pnpm
|
|
53
|
+
"release": "pnpm checktypes && pnpm build && pnpm publish --no-git-checks",
|
|
54
|
+
"release:rc": "pnpm checktypes && pnpm build && pnpm version prerelease --preid=rc"
|
|
58
55
|
}
|
|
59
56
|
}
|