Skip to content

Commit

Permalink
Merge pull request #19 from adidoesnt/development
Browse files Browse the repository at this point in the history
change search flow, add query message deletion
  • Loading branch information
adidoesnt authored Feb 7, 2024
2 parents 97691a4 + d3be8e9 commit 5fd15bc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
47 changes: 35 additions & 12 deletions telegram-bot/src/components/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,30 @@ 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!;
this.client.on('callback_query', async (query) => {
const { data: stringifiedData, message, from: queryFrom } = query;
const { chat, message_id: messageId } = message!;
const { id: queryFromId } = queryFrom;
const { id: chatId } = chat;
const data = JSON.parse(stringifiedData ?? '{}');
const { type, id: suggestionId } = data;
const { type, id: suggestionId, fromId } = data;
switch (type) {
case 'see_suggestion':
this.sendMessage(chatId, MESSAGE.CLOSE_MATCH, {
this.sendMessage(chatId, MESSAGE.JUMP, {
reply_to_message_id: suggestionId,
});
break;
default:
this.logger.error(ERROR.INVALID_CALLBACK_QUERY);
return;
}
if (queryFromId === Number(fromId)) {
try {
await this.client.deleteMessage(chatId, messageId);
} catch (error) {
this.logger.error(error);
}
}
});
this.logger.info('Bot initialised successfully');
}
Expand Down Expand Up @@ -152,25 +160,40 @@ export class Bot {
}
}

getInlineKeyboard(messageIds: number[]): Array<InlineKeyboardButton> {
getInlineKeyboard(messageIds: number[], fromId: string | 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,
fromId
}),
};
});
}

getResultString(documents: string[]): string {
let result = [MESSAGE.FOUND];
result.push(
documents
.map((doc: string, i: number) => {
const index = i + 1;
return `${index}. ${doc}`;
})
.join('\n'),
);
return result.join('\n');
}

async search(message: Message): Promise<void> {
const regex = new RegExp(/\/search/);
const {
chat_id: chatId,
message_content: messageContent,
message_id: messageId,
sender_id: fromId
} = this.getMessageMetadata(message);
try {
const query = messageContent.replace(regex, '').trim();
Expand All @@ -179,13 +202,13 @@ export class Bot {
chat_id: chatId,
});
const data = response;
const { ids: foundMessageIds } = data;
const foundMessageId = foundMessageIds?.shift();
if (foundMessageId) {
const { ids: foundMessageIds, documents } = data;
if (documents?.length > 0) {
const inlineKeyboardMarkup =
this.getInlineKeyboard(foundMessageIds);
await this.sendMessage(chatId, MESSAGE.FOUND, {
reply_to_message_id: foundMessageId,
this.getInlineKeyboard(foundMessageIds, fromId);
const reply = this.getResultString(documents);
await this.sendMessage(chatId, reply, {
reply_to_message_id: messageId,
reply_markup: {
inline_keyboard: [inlineKeyboardMarkup],
},
Expand Down
4 changes: 2 additions & 2 deletions telegram-bot/src/constants/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export const MESSAGE = {
PROMPT: 'Search usage: /search <search string>',
NO_RESULTS: 'No results found. Try a different search string.',
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.'
FOUND: 'These messages were the closest matches for your query. Which one would you like to jump to?',
JUMP: '^'
};

0 comments on commit 5fd15bc

Please sign in to comment.