Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds editMessageReplyMarkup method support #73

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/routes/bot/editMessageReplyMarkup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { handle } from './utils';
import type { Route } from '../route';

export const editMessageReplyMarkup: Route = (app, telegramServer) => {
handle(app, '/bot:token/editMessageReplyMarkup', (req, res, _next) => {
telegramServer.editMessageReplyMarkup(req.body);
const data = {ok: true, result: null};
res.sendResult(data);
});
};
2 changes: 2 additions & 0 deletions src/routes/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getMe } from './getMe';
import { answerCallbackQuery } from './answerCallbackQuery';
import { sendMessage } from './sendMessage';
import { editMessageText } from './editMessageText';
import { editMessageReplyMarkup } from './editMessageReplyMarkup';
import { setWebhook } from './setWebhook';
import { deleteWebhook } from './deleteWebhook';
import { unknownMethod } from './unknownMethod';
Expand All @@ -14,6 +15,7 @@ export const botRoutes = [
answerCallbackQuery,
getMe,
editMessageText,
editMessageReplyMarkup,
sendMessage,
setWebhook,
deleteWebhook,
Expand Down
19 changes: 19 additions & 0 deletions src/telegramServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface StoredUpdate {
type BotIncommingMessage = Params<'sendMessage', never>[0];

type BotEditTextIncommingMessage = Params<'editMessageText', never>[0];
type BotEditReplyMarkupIncommingMessage = Params<'editMessageReplyMarkup', never>[0];

type RawIncommingMessage = {
reply_markup?: string | object;
Expand Down Expand Up @@ -217,9 +218,27 @@ export class TelegramServer extends EventEmitter {
}
}

editMessageReplyMarkup(rawMessage: BotEditReplyMarkupIncommingMessage) {
const message = TelegramServer.normalizeMessage(rawMessage);
// only InlineKeyboardMarkup is allowed in response
if (message.reply_markup && 'inline_keyboard' in message.reply_markup) {
const update = this.storage.botMessages.find(
(u) =>(
String(u.messageId) === String(message.message_id)
&& String(u.message.chat_id) === String(message.chat_id)
),
);
if (update) {
update.message = {...update.message, ...message };
this.emit('EditedMessageReplyMarkup');
}
}
}

async waitBotEdits() {
return new Promise<void>((resolve) => {
this.once('EditedMessageText', () => resolve());
this.once('EditedMessageReplyMarkup', () => resolve());
});
}

Expand Down
56 changes: 56 additions & 0 deletions src/test/generic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,62 @@ describe('Telegram Server', () => {
await server.stop();
});

it('should handle reply markup editing', async () => {
const { server, client } = await getServerAndClient(token);
const bot = new TelegramBot(token, {baseApiUrl: server.config.apiURL, polling: true});
bot.onText(/\/start/, (msg) => {
const chatId = msg.from!.id;
bot.sendMessage(chatId, 'Greetings', {
reply_markup: {
inline_keyboard: [
[{
text: 'Button',
callback_data: 'button',
}],
],
},
});
});
bot.on('callback_query', (query) => {
if (query.data === 'edit_markup') {
bot.editMessageReplyMarkup(
{
inline_keyboard: [
[{
text: 'EditedButton',
callback_data: 'edited_button',
}],
],
},
{chat_id: query.message!.chat.id, message_id: query.message!.message_id},
);
}
});
await client.sendCommand(client.makeCommand('/start'));
const startUpdates = await client.getUpdates();
const botReply = startUpdates.result[0];
assert.exists(botReply);
const replyMarkup = botReply.message.reply_markup;
if (!isInlineKeyboard(replyMarkup)) {
assert.fail('Wrong keyboard type in request');
}
assert.equal(replyMarkup.inline_keyboard[0][0].text, 'Button');

const cb = client.makeCallbackQuery('edit_markup', {message: {message_id: botReply.messageId}});
await client.sendCallback(cb);
await server.waitBotEdits();
const allUpdates = await client.getUpdatesHistory();
const targetUpdate = allUpdates.find((update) => update.messageId === botReply.messageId);
assert.isTrue(!!targetUpdate && 'message' in targetUpdate);
const replyMarkupEdited = (targetUpdate as StoredBotUpdate).message.reply_markup;
if (!isInlineKeyboard(replyMarkupEdited)) {
assert.fail('Wrong keyboard type in stored update');
}
assert.equal(replyMarkupEdited.inline_keyboard[0][0].text, 'EditedButton');
await bot.stopPolling();
await server.stop();
});

it('should remove messages on storeTimeout', async () => {
const { server, client } = await getServerAndClient(token, {
storeTimeout: 1,
Expand Down