UNPKG

@great-detail/whatsapp

Version:

SDK for interfacing with WhatsApp Business Platform in Typescript or Node.js using the Cloud API, hosted by Meta.

2 lines (1 loc) 5.49 kB
import w from"ky";var f=class{constructor(e){this._request=e}getEndpoint(e){return encodeURIComponent(e)+"/whatsapp_business_profile"}getBusinessProfile({phoneNumberID:e,fields:t,request:o}){return w.create({...this._request,method:"GET",searchParams:{fields:Object.keys(t??{}).join(",")}})(this.getEndpoint(e),o)}updateBusinessProfile({phoneNumberID:e,request:t,...o}){return w.create({...this._request,method:"POST",json:{messaging_product:"whatsapp",...o}})(this.getEndpoint(e),t)}};import q from"ky";var g=class{constructor(e){this._request=e}getEndpoint(e){return encodeURIComponent(e)+"/media"}upload({phoneNumberID:e,file:t,filename:o,mimeType:n,request:l}){let a=new FormData;return a.set("messaging_product","whatsapp"),a.set("file",t,o),a.set("type",n),q.create({...this._request,method:"POST",body:a})(this.getEndpoint(e),l)}getURL({mediaID:e,phoneNumberID:t,request:o}){return q.create({...this._request,method:"GET",headers:{"Content-Type":"application/json"},searchParams:{...t?{phone_number_id:t}:{}}})(encodeURIComponent(e),o)}delete({mediaID:e,phoneNumberID:t,request:o}){return q.create({...this._request,method:"DELETE",headers:{"Content-Type":"application/json"},searchParams:{...t?{phone_number_id:t}:{}}})(encodeURIComponent(e),o)}download({mediaURL:e,request:t}){return q.create({method:"GET",headers:{"User-Agent":"node",Host:"lookaside.fbsbx.com"}})(e,t)}};import E from"ky";var O=class{constructor(e){this._request=e}getEndpoint(e){return encodeURIComponent(e)+"/messages"}createStatus({phoneNumberID:e,request:t,...o}){return E.create({...this._request,method:"POST",json:{messaging_product:"whatsapp",...o}})(this.getEndpoint(e),t)}createMessage({to:e,phoneNumberID:t,context:o,recipientType:n="individual",request:l,...a}){return E.create({...this._request,method:"POST",json:{messaging_product:"whatsapp",recipient_type:n,context:o,to:e,...a}})(this.getEndpoint(t),l)}};import R from"ky";var P=class{constructor(e){this._request=e}getEndpoint(e){return encodeURIComponent(e)+"/phone_numbers"}getPhoneNumber({phoneNumberID:e,fields:t,request:o}){return R.create({...this._request,method:"GET",searchParams:{fields:Object.keys(t??{}).join(",")}})(encodeURIComponent(e),o)}listPhoneNumbers({businessAccountID:e,sort:t,filtering:o,request:n}){return R.create({...this._request,method:"GET",searchParams:{...t?{sort:t}:{},...o?{filtering:o}:{}}})(this.getEndpoint(e),n)}};import N from"ky";var y=class{constructor(e){this._request=e}getEndpoint(e){return encodeURIComponent(e)+"/subscribed_apps"}createSubscription({businessAccountID:e,request:t}){return N.create({...this._request,method:"POST"})(this.getEndpoint(e),t)}listSubscriptions({businessAccountID:e,request:t}){return N.create({...this._request,method:"GET"})(this.getEndpoint(e),t)}};import{createHmac as G}from"crypto";var s=class extends Error{};var u=class extends s{};var d=class extends s{};var c=class extends s{};var p=class extends s{};var h=class extends s{};var m=class extends s{};var k=class{errors={WebhookError:s,IncorrectMethodWebhookError:u,InvalidHubChallengeWebhookError:d,InvalidHubModeWebhookError:c,InvalidHubSignatureWebhookError:p,InvalidHubVerifyTokenWebhookError:h,MissingBodyWebhookError:m};async register(e){if(e.method.toLowerCase()!=="get")throw new u("Webhook Registration Requests must use the GET request method.");let t=e.query["hub.mode"]??void 0;if(!t||t!=="subscribe")throw new c("Webhook Registration Request must have query parameter: hub.mode=subscribe");let o=e.query["hub.challenge"]??void 0;if(!o)throw new d("Webhook Registration Request must have query parameter: hub.challenge");let n=e.query["hub.verify_token"]??void 0;if(!n)throw new h("Webhook Registration Request must have query parameter: hub.verify_token");return{verifyToken:n,challenge:o,accept:()=>o,reject:()=>{}}}async eventNotification(e){if(e.method.toLowerCase()!=="post")throw new u("Webhook Event Notification Request must use the POST request method.");let t=e.headers["x-hub-signature"]?.toString().replace("sha1=",""),o=e.headers["x-hub-signature-256"]?.toString().replace("sha256=","");if(!o)throw new p("Webhook Event Notification Request must have header: x-hub-signature-256");if(!e.body)throw new m("Webhook Event Notification Request must have a body");let n=e.body,l=JSON.parse(n),a=b=>x=>G(b,x).update(n,"utf8").digest("hex"),S=(b,x)=>{let C=a(b);return v=>{let U=C(v);return x===U}};return{eventNotification:l,signature:{sha1:{value:t,getCalculatedSignature:a("sha1"),check:S("sha1",t)},sha256:{value:o,getCalculatedSignature:a("sha256"),check:S("sha256",o)}},checkSignature:S("sha256",o),verifySignature(b){if(!this.checkSignature(b))throw new p("Webhook Event Notification Signature doesn't match received body")},accept:()=>{}}}};var _=class{_request;businessProfile;message;phoneNumbers;subscribedApps;media;webhook;constructor({prefixUrl:e="https://graph.facebook.com",graphVersion:t="v20.0"}={}){this._request={prefixUrl:e.replace(/\/$/,"")+"/"+t},this.businessProfile=new f(this._request),this.message=new O(this._request),this.phoneNumbers=new P(this._request),this.subscribedApps=new y(this._request),this.media=new g(this._request),this.webhook=new k}};var M=(r=>(r.Audio="audio",r.Contacts="contacts",r.Document="document",r.Image="image",r.Interactive="interactive",r.Location="location",r.Reaction="sticker",r.Sticker="sticker",r.System="system",r.Button="button",r.Order="order",r.Template="template",r.Text="text",r.Video="video",r))(M||{});export{_ as a,M as b};