metamigo-api - fix linter errors in whatsapp service

This commit is contained in:
Abel Luck 2023-03-13 15:01:40 +00:00
parent ef216f7b1c
commit 8669b09224
7 changed files with 49 additions and 49 deletions

View file

@ -66,9 +66,8 @@ export const addTransactionCapability = (
return dict; return dict;
}, {}); }, {});
} }
return state.get(type, ids); return state.get(type, ids);
}, },
set(data) { set(data) {
if (inTransaction) { if (inTransaction) {

View file

@ -239,12 +239,11 @@ export const RequestCodeRoute = Helpers.withDefaults({
return h.response().code(402); return h.response().code(402);
} }
if (error.code) { if (error.code) {
return h.response().code(error.code); return h.response().code(error.code);
} }
return h.response().code(500); return h.response().code(500);
} }
}, },
}, },

View file

@ -10,7 +10,8 @@ import * as Helpers from "../../helpers";
import workerUtils from "../../../../worker-utils"; import workerUtils from "../../../../worker-utils";
import { SayLanguage, SayVoice } from "twilio/lib/twiml/VoiceResponse"; import { SayLanguage, SayVoice } from "twilio/lib/twiml/VoiceResponse";
const queueRecording = async (meta) => workerUtils.addJob("twilio-recording", meta, { jobKey: meta.callSid }); const queueRecording = async (meta) =>
workerUtils.addJob("twilio-recording", meta, { jobKey: meta.callSid });
const twilioClientFor = (provider: SavedVoiceProvider): Twilio.Twilio => { const twilioClientFor = (provider: SavedVoiceProvider): Twilio.Twilio => {
const { accountSid, apiKeySid, apiKeySecret } = provider.credentials; const { accountSid, apiKeySid, apiKeySecret } = provider.credentials;

View file

@ -1,4 +1,3 @@
import { Server } from "@hapi/hapi"; import { Server } from "@hapi/hapi";
import { Service } from "@hapipal/schmervice"; import { Service } from "@hapipal/schmervice";
import { import {

View file

@ -8,6 +8,7 @@ import makeWASocket, {
MediaType, MediaType,
AnyMessageContent, AnyMessageContent,
WAProto, WAProto,
UserFacingSocketConfig,
MiscMessageGenerationOptions, MiscMessageGenerationOptions,
} from "@adiwajshing/baileys"; } from "@adiwajshing/baileys";
import workerUtils from "../../worker-utils"; import workerUtils from "../../worker-utils";
@ -47,13 +48,16 @@ export default class WhatsappService extends Service {
private async sleep(ms: number): Promise<void> { private async sleep(ms: number): Promise<void> {
console.log(`pausing ${ms}`); console.log(`pausing ${ms}`);
return new Promise((resolve) => setTimeout(resolve, ms)); // eslint-disable-next-line no-new
new Promise((resolve) => {
setTimeout(resolve, ms);
});
} }
private async resetConnections() { private async resetConnections() {
for (const connection of Object.values(this.connections)) { for (const connection of Object.values(this.connections)) {
try { try {
connection.end(null); connection.end(undefined);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
@ -65,14 +69,14 @@ export default class WhatsappService extends Service {
private createConnection( private createConnection(
bot: Bot, bot: Bot,
server: Server, server: Server,
options: any, options: Omit<UserFacingSocketConfig, "auth">,
authCompleteCallback?: any authCompleteCallback?: () => void
) { ) {
const { state, saveState } = useDatabaseAuthState(bot, server); const { state, saveState } = useDatabaseAuthState(bot, server);
const connection = makeWASocket({ ...options, auth: state }); const connection = makeWASocket({ ...options, auth: state });
let pause = 5000; let pause = 5000;
connection.ev.on("connection.update", async (update) => { connection.ev.on("connection.update", async (update) => {
console.log(`Connection updated ${JSON.stringify(update, null, 2)}`); console.log(`Connection updated ${JSON.stringify(update, undefined, 2)}`);
const { const {
connection: connectionState, connection: connectionState,
lastDisconnect, lastDisconnect,
@ -88,6 +92,7 @@ export default class WhatsappService extends Service {
console.log("opened connection"); console.log("opened connection");
} else if (connectionState === "close") { } else if (connectionState === "close") {
console.log("connection closed due to", lastDisconnect.error); console.log("connection closed due to", lastDisconnect.error);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const disconnectStatusCode = (lastDisconnect?.error as any)?.output const disconnectStatusCode = (lastDisconnect?.error as any)?.output
?.statusCode; ?.statusCode;
if (disconnectStatusCode === DisconnectReason.restartRequired) { if (disconnectStatusCode === DisconnectReason.restartRequired) {
@ -146,16 +151,17 @@ export default class WhatsappService extends Service {
} }
} }
private async queueMessage(bot: Bot, webMessageInfo: proto.WebMessageInfo) { private async queueMessage(bot: Bot, webMessageInfo: proto.IWebMessageInfo) {
const { key, message, messageTimestamp } = webMessageInfo; const { key, message, messageTimestamp } = webMessageInfo;
const { remoteJid } = key; const { remoteJid } = key;
if (!key.fromMe && message && remoteJid !== "status@broadcast") { const {fromMe, id: keyId} = key;
if (!fromMe && message && remoteJid !== "status@broadcast") {
const { audioMessage, documentMessage, imageMessage, videoMessage } =
message;
const isMediaMessage = const isMediaMessage =
message.audioMessage || audioMessage || documentMessage || imageMessage || videoMessage;
message.documentMessage ||
message.imageMessage ||
message.videoMessage;
const messageContent = Object.values(message)[0]; const messageContent = Object.values(message)[0];
let messageType: MediaType; let messageType: MediaType;
@ -163,25 +169,22 @@ export default class WhatsappService extends Service {
let filename: string; let filename: string;
let mimetype: string; let mimetype: string;
if (isMediaMessage) { if (isMediaMessage) {
if (message.audioMessage) { if (audioMessage) {
messageType = "audio"; messageType = "audio";
filename = filename = keyId + "." + audioMessage.mimetype.split("/").pop();
key.id + "." + message.audioMessage.mimetype.split("/").pop(); mimetype = audioMessage.mimetype;
mimetype = message.audioMessage.mimetype; } else if (documentMessage) {
} else if (message.documentMessage) {
messageType = "document"; messageType = "document";
filename = message.documentMessage.fileName; filename = documentMessage.fileName;
mimetype = message.documentMessage.mimetype; mimetype = documentMessage.mimetype;
} else if (message.imageMessage) { } else if (imageMessage) {
messageType = "image"; messageType = "image";
filename = filename = keyId + "." + imageMessage.mimetype.split("/").pop();
key.id + "." + message.imageMessage.mimetype.split("/").pop(); mimetype = imageMessage.mimetype;
mimetype = message.imageMessage.mimetype; } else if (videoMessage) {
} else if (message.videoMessage) {
messageType = "video"; messageType = "video";
filename = filename = keyId + "." + videoMessage.mimetype.split("/").pop();
key.id + "." + message.videoMessage.mimetype.split("/").pop(); mimetype = videoMessage.mimetype;
mimetype = message.videoMessage.mimetype;
} }
const stream = await downloadContentFromMessage( const stream = await downloadContentFromMessage(
@ -198,7 +201,7 @@ export default class WhatsappService extends Service {
if (messageContent || attachment) { if (messageContent || attachment) {
const receivedMessage = { const receivedMessage = {
waMessageId: key.id, waMessageId: keyId,
waMessage: JSON.stringify(webMessageInfo), waMessage: JSON.stringify(webMessageInfo),
waTimestamp: new Date((messageTimestamp as number) * 1000), waTimestamp: new Date((messageTimestamp as number) * 1000),
attachment, attachment,
@ -209,13 +212,13 @@ export default class WhatsappService extends Service {
}; };
workerUtils.addJob("whatsapp-message", receivedMessage, { workerUtils.addJob("whatsapp-message", receivedMessage, {
jobKey: key.id, jobKey: keyId,
}); });
} }
} }
} }
private async queueUnreadMessages(bot: Bot, messages: any[]) { private async queueUnreadMessages(bot: Bot, messages: proto.IWebMessageInfo[]) {
for await (const message of messages) { for await (const message of messages) {
await this.queueMessage(bot, message); await this.queueMessage(bot, message);
} }
@ -274,7 +277,7 @@ export default class WhatsappService extends Service {
} }
} }
async receive(bot: Bot, lastReceivedDate: Date): Promise<any> { async receive(bot: Bot, _lastReceivedDate: Date): Promise<proto.IWebMessageInfo[]> {
const connection = this.connections[bot.id]; const connection = this.connections[bot.id];
// const messages = await connection.messagesReceivedAfter( // const messages = await connection.messagesReceivedAfter(
// lastReceivedDate, // lastReceivedDate,

View file

@ -1,7 +1,7 @@
export {
default,
loadConfig,
loadConfigRaw,
IAppConfig,
IAppConvict,
export {default, loadConfig, loadConfigRaw, IAppConfig, IAppConvict} from "@digiresilience/metamigo-config"; } from "@digiresilience/metamigo-config";

View file

@ -16,9 +16,8 @@ export const deployment = async (
return server; return server;
}; };
export const stopDeployment = async ( export const stopDeployment = async (server: Metamigo.Server): Promise<void> =>
server: Metamigo.Server Metamigo.stopDeployment(server);
): Promise<void> => Metamigo.stopDeployment(server);
const server = defState("server", { const server = defState("server", {
start: () => deployment(config, true), start: () => deployment(config, true),