Spaces:
Paused
Paused
Download src/message/message.controller.ts from Rasyiq03/scheffin-api: direct link, hf CLI and curl.
- Browser
- Download file 5.43 kB
-
https://huggingface.co/spaces/Rasyiq03/scheffin-api/resolve/main/src/message/message.controller.ts
- Command line
-
hf download hf://spaces/Rasyiq03/scheffin-api/src/message/message.controller.ts
-
curl -L -o message.controller.ts https://huggingface.co/spaces/Rasyiq03/scheffin-api/resolve/main/src/message/message.controller.ts
5.43 kB
| import { Body, Controller, Post, Res, HttpStatus } from '@nestjs/common'; | |
| import { WhatsappService } from '../whatsapp/whatsapp.service'; | |
| import { formatJid } from '../common/utils'; | |
| class TextMessageBody { | |
| target!: string; | |
| message!: string; | |
| } | |
| class MediaMessageBody { | |
| target!: string; | |
| url!: string; | |
| caption?: string; | |
| filename?: string; | |
| mimetype?: string; | |
| } | |
| class PollMessageBody { | |
| target!: string; | |
| question!: string; | |
| options!: string[]; | |
| multiSelect?: boolean; | |
| } | |
| ('send') | |
| export class MessageController { | |
| constructor(private readonly whatsappService: WhatsappService) {} | |
| private handleRouteError(err: unknown, res: any): void { | |
| const message = err instanceof Error ? err.message : 'Unknown error'; | |
| const statusCode = message.includes('belum terhubung') | |
| ? HttpStatus.SERVICE_UNAVAILABLE | |
| : HttpStatus.INTERNAL_SERVER_ERROR; | |
| console.error(`Error Pengiriman: ${message}`); | |
| res.status(statusCode).json({ status: 'Error', message }); | |
| } | |
| ('/') | |
| async sendText(() body: TextMessageBody, () res: any) { | |
| const { target, message } = body; | |
| if (!target || !message) { | |
| return res.status(HttpStatus.BAD_REQUEST).json({ | |
| status: 'Error', | |
| message: 'Field "target" dan "message" wajib diisi.', | |
| }); | |
| } | |
| try { | |
| const sock = this.whatsappService.getSocket(); | |
| const jid = formatJid(target); | |
| if (jid.endsWith('@g.us')) { | |
| await this.whatsappService.groupCacheReady; | |
| } | |
| // β οΈ PERHATIAN: Tidak Boleh menggunakan withTimeout di sini. | |
| // Jika disinkron ke 1000 anggota pertama kalinya dan butuh waktu 2 menit, | |
| // kita harus rela menunggu janji Baileys terpenuhi. | |
| // Terminate paksa promise ini hanya akan membuat mutex Baileys hang seumur hidup. | |
| await sock.sendMessage(jid, { text: message }); | |
| console.log(`π€ [TEXT] β ${jid} (Sukses)`); | |
| res | |
| .status(HttpStatus.OK) | |
| .json({ status: 'Success', type: 'text', to: jid }); | |
| } catch (err) { | |
| this.handleRouteError(err, res); | |
| } | |
| } | |
| ('/image') | |
| async sendImage(() body: MediaMessageBody, () res: any) { | |
| const { target, url, caption } = body; | |
| if (!target || !url) { | |
| return res | |
| .status(HttpStatus.BAD_REQUEST) | |
| .json({ status: 'Error', message: 'target & url wajib.' }); | |
| } | |
| try { | |
| const sock = this.whatsappService.getSocket(); | |
| const jid = formatJid(target); | |
| if (jid.endsWith('@g.us')) await this.whatsappService.groupCacheReady; | |
| await sock.sendMessage(jid, { image: { url }, caption: caption ?? '' }); | |
| console.log(`π€ [IMAGE] β ${jid}`); | |
| res | |
| .status(HttpStatus.OK) | |
| .json({ status: 'Success', type: 'image', to: jid }); | |
| } catch (err) { | |
| this.handleRouteError(err, res); | |
| } | |
| } | |
| ('/video') | |
| async sendVideo(() body: MediaMessageBody, () res: any) { | |
| const { target, url, caption } = body; | |
| if (!target || !url) { | |
| return res | |
| .status(HttpStatus.BAD_REQUEST) | |
| .json({ status: 'Error', message: 'target & url wajib.' }); | |
| } | |
| try { | |
| const sock = this.whatsappService.getSocket(); | |
| const jid = formatJid(target); | |
| if (jid.endsWith('@g.us')) await this.whatsappService.groupCacheReady; | |
| await sock.sendMessage(jid, { video: { url }, caption: caption ?? '' }); | |
| console.log(`π€ [VIDEO] β ${jid}`); | |
| res | |
| .status(HttpStatus.OK) | |
| .json({ status: 'Success', type: 'video', to: jid }); | |
| } catch (err) { | |
| this.handleRouteError(err, res); | |
| } | |
| } | |
| ('/document') | |
| async sendDocument(() body: MediaMessageBody, () res: any) { | |
| const { target, url, filename, mimetype } = body; | |
| if (!target || !url || !filename) { | |
| return res | |
| .status(HttpStatus.BAD_REQUEST) | |
| .json({ status: 'Error', message: 'target, url & filename wajib.' }); | |
| } | |
| try { | |
| const sock = this.whatsappService.getSocket(); | |
| const jid = formatJid(target); | |
| if (jid.endsWith('@g.us')) await this.whatsappService.groupCacheReady; | |
| await sock.sendMessage(jid, { | |
| document: { url }, | |
| fileName: filename, | |
| mimetype: mimetype ?? 'application/octet-stream', | |
| }); | |
| console.log(`π€ [DOC] β ${jid}`); | |
| res | |
| .status(HttpStatus.OK) | |
| .json({ status: 'Success', type: 'document', to: jid }); | |
| } catch (err) { | |
| this.handleRouteError(err, res); | |
| } | |
| } | |
| ('/poll') | |
| async sendPoll(() body: PollMessageBody, () res: any) { | |
| const { target, question, options, multiSelect } = body; | |
| if (!target || !question || !options || options.length < 2) { | |
| return res | |
| .status(HttpStatus.BAD_REQUEST) | |
| .json({ status: 'Error', message: 'Opsi poll minimal 2' }); | |
| } | |
| try { | |
| const sock = this.whatsappService.getSocket(); | |
| const jid = formatJid(target); | |
| if (jid.endsWith('@g.us')) await this.whatsappService.groupCacheReady; | |
| await sock.sendMessage(jid, { | |
| poll: { | |
| name: question, | |
| values: options, | |
| selectableCount: multiSelect ? options.length : 1, | |
| }, | |
| }); | |
| console.log(`π€ [POLL] β ${jid}`); | |
| res | |
| .status(HttpStatus.OK) | |
| .json({ status: 'Success', type: 'poll', to: jid }); | |
| } catch (err) { | |
| this.handleRouteError(err, res); | |
| } | |
| } | |
| } | |