Skip to content

Commit

Permalink
Merge pull request #17 from adidoesnt/development
Browse files Browse the repository at this point in the history
Flatten IDs
  • Loading branch information
adidoesnt authored Feb 7, 2024
2 parents d034295 + 46f1463 commit 6d3b7bd
Show file tree
Hide file tree
Showing 23 changed files with 199 additions and 288 deletions.
1 change: 1 addition & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async def search_messages(search_string: str = Query(..., min_length=1), chat_id
n_results=5
)

results["ids"] = results["ids"][0]
results["documents"] = results["documents"][0]

return results
Expand Down
3 changes: 0 additions & 3 deletions whatsapp-bot/.gitignore → health/.gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# WhatsApp Web
.wwebjs_cache

# Logs

logs
Expand Down
14 changes: 14 additions & 0 deletions health/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM oven/bun

WORKDIR /app

COPY package.json .
COPY bun.lockb .

RUN bun install

COPY . .

EXPOSE 3000

CMD ["bun", "run", "start"]
22 changes: 9 additions & 13 deletions whatsapp-bot/README.md → health/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# EchoFinder WhatsApp Bot

This is the EchoFinder Telegram bot. It allows you to search for messages, and returns results based on similarity.
# Health Check

## Setup

Expand All @@ -12,15 +10,6 @@ To install dependencies:
bun install
```

### Environment Variables

```bash
BOT_CHAT_IDS= # comma separated, no spaces
DEBUG_MODE=
```

### Running

To run:

```bash
Expand All @@ -31,6 +20,13 @@ bun run start
bun run dev
```

## Acknowledgements
### Environment Variables

```bash
SERVER_LIST=
PING_INTERVAL=
LOG_FILE=
ERROR_LOG_FILE=
```

This project was created using `bun init` in bun v1.0.25. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
Binary file added health/bun.lockb
Binary file not shown.
22 changes: 22 additions & 0 deletions health/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "ping",
"module": "src/index.ts",
"type": "module",
"scripts": {
"start": "bun run src/index.ts",
"dev": "bun run --hot src/index.ts",
"build": "docker build -t echofinder-health:latest .",
"local": "docker run --env-file .env echofinder-health:latest",
"prod": "docker run echofinder-health:latest"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"axios": "^1.6.7",
"log4js": "^6.9.1"
}
}
23 changes: 23 additions & 0 deletions health/src/components/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import log4js from "log4js";

const {
LOG_FILE: logFile = "logs/combined.log",
ERROR_LOG_FILE: errorFile = "logs/error.log",
} = process.env;

log4js.configure({
appenders: {
console: { type: "console" },
file: { type: "file", filename: logFile },
error: { type: "file", filename: errorFile },
},
categories: {
default: { appenders: ["console", "file"], level: "info" },
error: { appenders: ["console", "error", "file"], level: "error" },
},
});

export const logger = {
defaultLogger: log4js.getLogger("default"),
errorLogger: log4js.getLogger("error"),
};
31 changes: 31 additions & 0 deletions health/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import axios, { AxiosError } from "axios";
import { logger } from "components/logger";

const { SERVER_LIST = "", PING_INTERVAL = "300000" } = process.env;
const { defaultLogger, errorLogger } = logger;
const servers: Array<string> = SERVER_LIST.split(",");
const pingInterval = Number(PING_INTERVAL);

const pingServers = async () => {
const promises = servers.map(async (server: string) => {
defaultLogger.info(`Checking server health ${server}`);
try {
await axios.get(server);
} catch (error) {
if ((error as AxiosError).response?.status) {
defaultLogger.info(`Server at ${server} is healthy.`);
} else {
errorLogger.error(`Server at ${server} is down.`);
}
}
});
await Promise.all(promises);
};

defaultLogger.info(
`Starting health check for servers ${servers.join(", ")}...`
);
setTimeout(
() => pingServers().then(() => setInterval(pingServers, pingInterval)),
3000
);
23 changes: 23 additions & 0 deletions health/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
"baseUrl": "./src",

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

/* Linting */
"skipLibCheck": true,
"strict": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
}
}
Binary file modified telegram-bot/bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions telegram-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"devDependencies": {
"@types/bun": "latest",
"@types/express": "^4.17.21",
"@types/node-telegram-bot-api": "^0.64.2",
"prettier": "^3.2.5"
},
Expand Down
100 changes: 71 additions & 29 deletions telegram-bot/src/components/bot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { type Logger as Log4js } from 'log4js';
import Client, { type Message } from 'node-telegram-bot-api';
import Client, {
type InlineKeyboardButton,
type Message,
type SendMessageOptions,
} from 'node-telegram-bot-api';
import { Logger } from 'components/logger';
import { MESSAGE } from 'constants/message';
import { ERROR } from 'constants/error';
Expand Down Expand Up @@ -51,6 +55,23 @@ export class Bot {
this.client.on('message', (message: Message) => {
this.saveMessage(message);
});
this.client.on('callback_query', (query) => {
const { data: stringifiedData, message } = query;
const { chat } = message!;
const { id: chatId } = chat;
const data = JSON.parse(stringifiedData ?? '{}');
const { type, id: suggestionId } = data;
switch (type) {
case 'see_suggestion':
this.sendMessage(chatId, MESSAGE.CLOSE_MATCH, {
reply_to_message_id: suggestionId,
});
break;
default:
this.logger.error(ERROR.INVALID_CALLBACK_QUERY);
return;
}
});
this.logger.info('Bot initialised successfully');
}

Expand Down Expand Up @@ -87,12 +108,16 @@ export class Bot {

async help(message: Message) {
const { chat_id, message_id } = this.getMessageMetadata(message);
await this.sendMessage(chat_id, MESSAGE.HELP, message_id);
await this.sendMessage(chat_id, MESSAGE.HELP, {
reply_to_message_id: message_id,
});
}

async prompt(message: Message) {
const { chat_id, message_id } = this.getMessageMetadata(message);
await this.sendMessage(chat_id, MESSAGE.PROMPT, message_id);
await this.sendMessage(chat_id, MESSAGE.PROMPT, {
reply_to_message_id: message_id,
});
}

validateMesssage(message: Message): boolean {
Expand All @@ -102,18 +127,15 @@ export class Bot {
}

async sendMessage(
chatId: string,
chatId: string | number,
message: string,
message_id?: string,
): Promise<void> {
this.logger.info(`Sending message to chat ${chatId}`, { message });
if (message_id) {
this.client.sendMessage(chatId, message, {
reply_to_message_id: Number(message_id),
});
} else {
this.client.sendMessage(chatId, message);
}
options?: Partial<SendMessageOptions>,
): Promise<Message> {
this.logger.info(`Sending message to chat ${chatId}`, {
message,
options,
});
return this.client.sendMessage(chatId, message, options);
}

async saveMessage(message: Message): Promise<void> {
Expand All @@ -130,29 +152,49 @@ export class Bot {
}
}

getInlineKeyboard(messageIds: number[]): Array<InlineKeyboardButton> {
return messageIds.map((id: number, i: number) => {
const index = i + 1;
return {
text: `${index}`,
callback_data: JSON.stringify({
type: 'see_suggestion',
id,
}),
};
});
}

async search(message: Message): Promise<void> {
const regex = new RegExp(/\/search/);
const { chat_id, message_content, message_id } =
this.getMessageMetadata(message);
const {
chat_id: chatId,
message_content: messageContent,
message_id: messageId,
} = this.getMessageMetadata(message);
try {
const query = message_content.replace(regex, '').trim();
const query = messageContent.replace(regex, '').trim();
const response = await this.apiClient.get('/messages/search', {
search_string: query,
chat_id,
chat_id: chatId,
});
const data = response;
const { documents } = data;
const results = documents
.map((doc: string, i: number) => {
const index = i + 1;
return `${index}. ${doc}`;
})
.join('\n');
if (!results || results.length === 0) {
await this.sendMessage(chat_id, MESSAGE.NO_RESULTS, message_id);
return;
const { ids: foundMessageIds } = data;
const foundMessageId = foundMessageIds[0]?.shift();
if (foundMessageId) {
const inlineKeyboardMarkup =
this.getInlineKeyboard(foundMessageIds[0]);
await this.sendMessage(chatId, MESSAGE.FOUND, {
reply_to_message_id: foundMessageId,
reply_markup: {
inline_keyboard: [inlineKeyboardMarkup],
},
});
} else {
await this.sendMessage(chatId, MESSAGE.NO_RESULTS, {
reply_to_message_id: messageId,
});
}
this.sendMessage(chat_id, results, message_id);
} catch (error) {
this.logger.error(error);
}
Expand Down
1 change: 1 addition & 0 deletions telegram-bot/src/constants/error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const ERROR = {
NO_FROM: 'No from field in message',
INVALID_CALLBACK_QUERY: 'Invalid callback query',
};
4 changes: 3 additions & 1 deletion telegram-bot/src/constants/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ export const MESSAGE = {
HELP: 'This is the EchoFinder Telegram bot. It allows you to search for messages, and returns results based on similarity. Usage: /search <search string>',
PROMPT: 'Search usage: /search <search string>',
NO_RESULTS: 'No results found. Try a different search string.',
PROCESSING_UPDATE: 'Processing update'
PROCESSING_UPDATE: 'Processing update',
FOUND: 'We found this message that we think matches your search! If it wasn\'t the message you were looking for, the buttons below correspond to the next 4 closest matches.',
CLOSE_MATCH: 'This message was also a close match to your query.'
};
7 changes: 0 additions & 7 deletions whatsapp-bot/.prettierrc

This file was deleted.

Binary file removed whatsapp-bot/bun.lockb
Binary file not shown.
25 changes: 0 additions & 25 deletions whatsapp-bot/package.json

This file was deleted.

Loading

0 comments on commit 6d3b7bd

Please sign in to comment.