Skip to content

Commit

Permalink
perf: editMessageMedia on text
Browse files Browse the repository at this point in the history
previously this needed to be recreated. Now this can be edited onto the same message.

added in Bot API 7.11
  • Loading branch information
EdJoPaTo committed Oct 31, 2024
1 parent 4262997 commit a06e1a1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion source/send-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ export async function editMenuOnContext<Context extends BaseContext>(
if (isMediaBody(body)) {
if (
'animation' in message
|| 'audio' in message
|| 'document' in message
|| 'audio' in message
|| 'photo' in message
|| 'video' in message
|| 'text' in message
) {
return context.editMessageMedia(
{
Expand Down
49 changes: 47 additions & 2 deletions test/send-menu/context-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ await test('context-edit media reply when not a callback query', async t => {
strictEqual(replyWithPhoto.mock.callCount(), 1);
});

await test('context-edit media reply when text message', async t => {
await test('context-edit media reply when location message', async t => {
const menu = new MenuTemplate<BaseContext>({
media: 'whatever',
type: 'photo',
Expand Down Expand Up @@ -252,7 +252,10 @@ await test('context-edit media reply when text message', async t => {
message_id: 666,
date: 666,
chat: undefined as any,
text: 'whatever',
location: {
latitude: 13,
longitude: 37,
},
},
},
deleteMessage,
Expand Down Expand Up @@ -306,6 +309,48 @@ await test('context-edit media edit when media message', async t => {
strictEqual(editMessageMedia.mock.callCount(), 1);
});

await test('context-edit media edit when text message', async t => {
const menu = new MenuTemplate<BaseContext>({
media: 'whatever',
type: 'photo',
});

const editMessageMedia = t.mock.fn<BaseContext['editMessageMedia']>(
async (media, other) => {
deepStrictEqual(media, {
media: 'whatever',
type: 'photo',
caption: undefined,
parse_mode: undefined,
});
deepStrictEqual(other, {
reply_markup: {
inline_keyboard: [],
},
});
return undefined as any;
},
);
const fakeContext: Partial<BaseContext> = {
callbackQuery: {
id: '666',
from: undefined as any,
chat_instance: '666',
data: '666',
message: {
message_id: 666,
date: 666,
chat: undefined as any,
text: 'whatever',
},
},
editMessageMedia,
};

await editMenuOnContext(menu, fakeContext as any, '/');
strictEqual(editMessageMedia.mock.callCount(), 1);
});

await test('context-edit does not throw message is not modified', async t => {
const menu = new MenuTemplate<BaseContext>('whatever');

Expand Down

0 comments on commit a06e1a1

Please sign in to comment.