Skip to content

Commit

Permalink
feat(whatsapp): internal api
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Aug 12, 2023
1 parent 28414e9 commit fc1cb99
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion adapters/whatsapp/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class WhatsAppAdapter extends Adapter<WhatsAppBot> {
const bot = this.bots.find((bot) => bot.selfId === selfId)
if (!bot) return ctx.status = 404

const fetched = await bot.http.get<{ url: string }>('/' + mediaId)
const fetched = await bot.internal.getMedia(mediaId)
this.logger.debug(fetched.url)
const resp = await bot.ctx.http.axios<internal.Readable>({
url: fetched.url,
Expand Down
24 changes: 23 additions & 1 deletion adapters/whatsapp/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Quester } from '@satorijs/satori'
import { SendMessage } from './types'
import FormData from 'form-data'

interface PhoneNumber {
verified_name: string
Expand All @@ -9,9 +11,10 @@ interface PhoneNumber {
}

export class Internal {
constructor(public http: Quester) {}
constructor(public http: Quester) { }

async getPhoneNumbers(id: string) {
// https://developers.facebook.com/docs/whatsapp/business-management-api/manage-phone-numbers#all-phone-numbers
const { data } = await this.http.get<{ data: PhoneNumber[] }>(`/${id}/phone_numbers`)
return data
}
Expand All @@ -29,4 +32,23 @@ export class Internal {
},
})
}

async sendMessage(selfId: string, data: SendMessage) {
const response = await this.http.post<{
messages: { id: string }[]
}>(`/${selfId}/messages`, data)
return response
}

getMedia(mediaId: string) {
return this.http.get<{ url: string }>('/' + mediaId)
}

uploadMedia(selfId: string, form: FormData) {
return this.http.post<{
id: string
}>(`/${selfId}/media`, form, {
headers: form.getHeaders(),
})
}
}
13 changes: 3 additions & 10 deletions adapters/whatsapp/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export class WhatsAppMessageEncoder extends MessageEncoder<WhatsAppBot> {
if (type === 'text' && !this.buffer.length) return
if (type !== 'text' && this.buffer.length) await this.flushTextMessage()
// https://developers.facebook.com/docs/whatsapp/api/messages/text
const { messages } = await this.bot.http.post<{
messages: { id: string }[]
}>(`/${this.bot.selfId}/messages`, {
const { messages } = await this.bot.internal.sendMessage(this.bot.selfId, {
messaging_product: 'whatsapp',
to: this.channelId,
recipient_type: 'individual',
Expand All @@ -55,8 +53,7 @@ export class WhatsAppMessageEncoder extends MessageEncoder<WhatsAppBot> {
message_id: this.quoteId,
},
} : {}),
})

} as SendMessage)
for (const msg of messages) {
const session = this.bot.session()
session.type = 'message'
Expand Down Expand Up @@ -92,11 +89,7 @@ export class WhatsAppMessageEncoder extends MessageEncoder<WhatsAppBot> {
form.append('type', mime)
form.append('messaging_product', 'whatsapp')

const r = await this.bot.http.post<{
id: string
}>(`/${this.bot.selfId}/media`, form, {
headers: form.getHeaders(),
})
const r = await this.bot.internal.uploadMedia(this.bot.selfId, form)
return r.id
}

Expand Down

0 comments on commit fc1cb99

Please sign in to comment.