@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.5 kB
JavaScript
import C from"ky";var y=class{constructor(e){this._transport=e}getEndpoint(e){return encodeURIComponent(e)+"/whatsapp_business_profile"}getBusinessProfile({phoneNumberID:e,fields:t,request:o}){return this._transport.extend({method:"GET",searchParams:{fields:Object.keys(t??{}).join(",")}})(this.getEndpoint(e),o)}updateBusinessProfile({phoneNumberID:e,request:t,...o}){return this._transport.extend({method:"POST",json:{messaging_product:"whatsapp",...o}})(this.getEndpoint(e),t)}};import I from"ky";var g=class{constructor(e){this._transport=e}getEndpoint(e){return encodeURIComponent(e)+"/media"}upload({phoneNumberID:e,file:t,filename:o,mimeType:i,request:f}){let a=new FormData;return a.set("messaging_product","whatsapp"),a.set("file",t,o),a.set("type",i),this._transport.extend({method:"POST",body:a})(this.getEndpoint(e),f)}getURL({mediaID:e,phoneNumberID:t,request:o}){return this._transport.extend({method:"GET",headers:{"Content-Type":"application/json"},searchParams:{...t?{phone_number_id:t}:{}}})(encodeURIComponent(e),o)}delete({mediaID:e,phoneNumberID:t,request:o}){return this._transport.extend({method:"DELETE",headers:{"Content-Type":"application/json"},searchParams:{...t?{phone_number_id:t}:{}}})(encodeURIComponent(e),o)}download({mediaURL:e,request:t}){return I.create({method:"GET",headers:{"User-Agent":"node",Host:"lookaside.fbsbx.com"}})(e,t)}};var x=class{constructor(e){this._transport=e}getEndpoint(e){return encodeURIComponent(e)+"/messages"}createStatus({phoneNumberID:e,request:t,...o}){return this._transport.extend({method:"POST",json:{messaging_product:"whatsapp",...o}})(this.getEndpoint(e),t)}createMessage({to:e,phoneNumberID:t,context:o,recipientType:i="individual",request:f,...a}){return this._transport.extend({method:"POST",json:{messaging_product:"whatsapp",recipient_type:i,context:o,to:e,...a}})(this.getEndpoint(t),f)}};var O=class{constructor(e){this._transport=e}getEndpoint(e){return encodeURIComponent(e)+"/phone_numbers"}getPhoneNumber({phoneNumberID:e,fields:t,request:o}){return this._transport.extend({method:"GET",searchParams:{fields:Object.keys(t??{}).join(",")}})(encodeURIComponent(e),o)}listPhoneNumbers({businessAccountID:e,sort:t,filtering:o,request:i}){return this._transport.extend({method:"GET",searchParams:{...t?{sort:t}:{},...o?{filtering:o}:{}}})(this.getEndpoint(e),i)}};var P=class{constructor(e){this._transport=e}getEndpoint(e){return encodeURIComponent(e)+"/subscribed_apps"}createSubscription({businessAccountID:e,request:t}){return this._transport.extend({method:"POST"})(this.getEndpoint(e),t)}listSubscriptions({businessAccountID:e,request:t}){return this._transport.extend({method:"GET"})(this.getEndpoint(e),t)}};import{createHmac as N}from"crypto";var s=class extends Error{};var p=class extends s{};var u=class extends s{};var c=class extends s{};var d=class extends s{};var m=class extends s{};var h=class extends s{};var M=class{errors={WebhookError:s,IncorrectMethodWebhookError:p,InvalidHubChallengeWebhookError:u,InvalidHubModeWebhookError:c,InvalidHubSignatureWebhookError:d,InvalidHubVerifyTokenWebhookError:m,MissingBodyWebhookError:h};async register(e){if(e.method.toLowerCase()!=="get")throw new p("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 u("Webhook Registration Request must have query parameter: hub.challenge");let i=e.query["hub.verify_token"]??void 0;if(!i)throw new m("Webhook Registration Request must have query parameter: hub.verify_token");return{verifyToken:i,challenge:o,accept:()=>o,reject:()=>{}}}async eventNotification(e){if(e.method.toLowerCase()!=="post")throw new p("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 d("Webhook Event Notification Request must have header: x-hub-signature-256");if(!e.body)throw new h("Webhook Event Notification Request must have a body");let i=e.body,f=JSON.parse(i),a=b=>k=>N(b,k).update(i,"utf8").digest("hex"),S=(b,k)=>{let E=a(b);return _=>{let w=E(_);return k===w}};return{eventNotification:f,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 d("Webhook Event Notification Signature doesn't match received body")},accept:()=>{}}}};var l=class{_transport;businessProfile;message;phoneNumbers;subscribedApps;media;webhook;constructor({prefixUrl:e="https://graph.facebook.com",graphVersion:t="v23.0",request:o}={}){this._transport=C.create({...o,prefixUrl:e.replace(/\/$/,"")+"/"+t}),this.businessProfile=new y(this._transport),this.message=new x(this._transport),this.phoneNumbers=new O(this._transport),this.subscribedApps=new P(this._transport),this.media=new g(this._transport),this.webhook=new M}};var R=(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))(R||{});export{l as Client,l as CloudAPI,R as MessageType,l as default};