From fc1cb99506aa0821171491b96fa09d5940addeba Mon Sep 17 00:00:00 2001 From: LittleC Date: Sat, 12 Aug 2023 14:51:43 +0800 Subject: [PATCH] feat(whatsapp): internal api --- adapters/whatsapp/src/adapter.ts | 2 +- adapters/whatsapp/src/internal.ts | 24 +++++++++++++++++++++++- adapters/whatsapp/src/message.ts | 13 +++---------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/adapters/whatsapp/src/adapter.ts b/adapters/whatsapp/src/adapter.ts index e6a0e832..881ee57b 100644 --- a/adapters/whatsapp/src/adapter.ts +++ b/adapters/whatsapp/src/adapter.ts @@ -78,7 +78,7 @@ export class WhatsAppAdapter extends Adapter { 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({ url: fetched.url, diff --git a/adapters/whatsapp/src/internal.ts b/adapters/whatsapp/src/internal.ts index 7386f150..d6d5bc06 100644 --- a/adapters/whatsapp/src/internal.ts +++ b/adapters/whatsapp/src/internal.ts @@ -1,4 +1,6 @@ import { Quester } from '@satorijs/satori' +import { SendMessage } from './types' +import FormData from 'form-data' interface PhoneNumber { verified_name: string @@ -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 } @@ -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(), + }) + } } diff --git a/adapters/whatsapp/src/message.ts b/adapters/whatsapp/src/message.ts index 33950d33..43ea6113 100644 --- a/adapters/whatsapp/src/message.ts +++ b/adapters/whatsapp/src/message.ts @@ -42,9 +42,7 @@ export class WhatsAppMessageEncoder extends MessageEncoder { 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', @@ -55,8 +53,7 @@ export class WhatsAppMessageEncoder extends MessageEncoder { message_id: this.quoteId, }, } : {}), - }) - + } as SendMessage) for (const msg of messages) { const session = this.bot.session() session.type = 'message' @@ -92,11 +89,7 @@ export class WhatsAppMessageEncoder extends MessageEncoder { 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 }