Skip to content

Commit

Permalink
style: use semicolon
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Nov 27, 2023
1 parent f533351 commit 7c24bc5
Show file tree
Hide file tree
Showing 69 changed files with 3,034 additions and 3,022 deletions.
156 changes: 78 additions & 78 deletions examples/main-javascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,138 +4,138 @@
//
// If you just want to use JavaScript, go ahead :)

import * as process from 'node:process'
import {Bot} from 'grammy'
import * as process from 'node:process';
import {Bot} from 'grammy';
import {
createBackMainMenuButtons,
MenuMiddleware,
MenuTemplate,
} from '../dist/source/index.js'
} from '../dist/source/index.js';

const menu = new MenuTemplate(() => 'Main Menu\n' + new Date().toISOString())
const menu = new MenuTemplate(() => 'Main Menu\n' + new Date().toISOString());

menu.url('EdJoPaTo.de', 'https://edjopato.de')
menu.url('EdJoPaTo.de', 'https://edjopato.de');

let mainMenuToggle = false
let mainMenuToggle = false;
menu.toggle('toggle me', 'toggle me', {
set(_, newState) {
mainMenuToggle = newState
mainMenuToggle = newState;
// Update the menu afterwards
return true
return true;
},
isSet: () => mainMenuToggle,
})
});

menu.interact('interaction', 'interact', {
hide: () => mainMenuToggle,
async do(ctx) {
await ctx.answerCallbackQuery({text: 'you clicked me!'})
await ctx.answerCallbackQuery({text: 'you clicked me!'});
// Do not update the menu afterwards
return false
return false;
},
})
});

menu.interact('update after action', 'update afterwards', {
joinLastRow: true,
hide: () => mainMenuToggle,
async do(ctx) {
await ctx.answerCallbackQuery({text: 'I will update the menu now…'})
await ctx.answerCallbackQuery({text: 'I will update the menu now…'});

return true
return true;

// You can return true to update the same menu or use a relative path
// For example '.' for the same menu or '..' for the parent menu
// return '.'
},
})
});

let selectedKey = 'b'
let selectedKey = 'b';
menu.select('select', ['A', 'B', 'C'], {
async set(ctx, key) {
selectedKey = key
await ctx.answerCallbackQuery({text: `you selected ${key}`})
return true
selectedKey = key;
await ctx.answerCallbackQuery({text: `you selected ${key}`});
return true;
},
isSet: (_, key) => key === selectedKey,
})
});

const foodMenu = new MenuTemplate(
'People like food. What do they like?',
)
);

const people = {Mark: {}, Paul: {}}
const food = ['bread', 'cake', 'bananas']
const people = {Mark: {}, Paul: {}};
const food = ['bread', 'cake', 'bananas'];

function personButtonText(_, key) {
const entry = people[key]
const entry = people[key];
if (entry?.food) {
return `${key} (${entry.food})`
return `${key} (${entry.food})`;
}

return key
return key;
}

function foodSelectText(ctx) {
const person = ctx.match[1]
const hisChoice = people[person].food
const person = ctx.match[1];
const hisChoice = people[person].food;
if (!hisChoice) {
return `${person} is still unsure what to eat.`
return `${person} is still unsure what to eat.`;
}

return `${person} likes ${hisChoice} currently.`
return `${person} likes ${hisChoice} currently.`;
}

const foodSelectSubmenu = new MenuTemplate(foodSelectText)
const foodSelectSubmenu = new MenuTemplate(foodSelectText);
foodSelectSubmenu.toggle('Prefer tea', 'tea', {
set(ctx, choice) {
const person = ctx.match[1]
people[person].tee = choice
return true
const person = ctx.match[1];
people[person].tee = choice;
return true;
},
isSet(ctx) {
const person = ctx.match[1]
return people[person].tee === true
const person = ctx.match[1];
return people[person].tee === true;
},
})
});
foodSelectSubmenu.select('food', food, {
set(ctx, key) {
const person = ctx.match[1]
people[person].food = key
return true
const person = ctx.match[1];
people[person].food = key;
return true;
},
isSet(ctx, key) {
const person = ctx.match[1]
return people[person].food === key
const person = ctx.match[1];
return people[person].food === key;
},
})
foodSelectSubmenu.manualRow(createBackMainMenuButtons())
});
foodSelectSubmenu.manualRow(createBackMainMenuButtons());

foodMenu.chooseIntoSubmenu('person', () => Object.keys(people), foodSelectSubmenu, {
buttonText: personButtonText,
columns: 2,
})
foodMenu.manualRow(createBackMainMenuButtons())
});
foodMenu.manualRow(createBackMainMenuButtons());

menu.submenu('Food menu', 'food', foodMenu, {
hide: () => mainMenuToggle,
})
});

let mediaOption = 'photo1'
let mediaOption = 'photo1';
const mediaMenu = new MenuTemplate(() => {
if (mediaOption === 'video') {
return {
type: 'video',
media: 'https://telegram.org/img/t_main_Android_demo.mp4',
text: 'Just a caption for a video',
}
};
}

if (mediaOption === 'animation') {
return {
type: 'animation',
media: 'https://telegram.org/img/t_main_Android_demo.mp4',
text: 'Just a caption for an animation',
}
};
}

if (mediaOption === 'photo2') {
Expand All @@ -144,7 +144,7 @@ const mediaMenu = new MenuTemplate(() => {
media: 'https://telegram.org/img/SiteAndroid.jpg',
text: 'Just a caption for a *photo*',
parse_mode: 'Markdown',
}
};
}

if (mediaOption === 'document') {
Expand All @@ -153,7 +153,7 @@ const mediaMenu = new MenuTemplate(() => {
media: 'https://telegram.org/file/464001088/1/bI7AJLo7oX4.287931.zip/374fe3b0a59dc60005',
text: 'Just a caption for a <b>document</b>',
parse_mode: 'HTML',
}
};
}

if (mediaOption === 'location') {
Expand All @@ -164,7 +164,7 @@ const mediaMenu = new MenuTemplate(() => {
longitude: 10,
},
live_period: 60,
}
};
}

if (mediaOption === 'venue') {
Expand All @@ -177,65 +177,65 @@ const mediaMenu = new MenuTemplate(() => {
title: 'simple coordinates point',
address: 'Hamburg, Germany',
},
}
};
}

if (mediaOption === 'just text') {
return {
text: 'Just some text',
}
};
}

return {
type: 'photo',
media: 'https://telegram.org/img/SiteiOs.jpg',
}
})
};
});
mediaMenu.interact('Just a button', 'randomButton', {
async do(ctx) {
await ctx.answerCallbackQuery({text: 'Just a callback query answer'})
return false
await ctx.answerCallbackQuery({text: 'Just a callback query answer'});
return false;
},
})
});
mediaMenu.select('type', ['animation', 'document', 'photo1', 'photo2', 'video', 'location', 'venue', 'just text'], {
columns: 2,
isSet: (_, key) => mediaOption === key,
set(_, key) {
mediaOption = key
return true
mediaOption = key;
return true;
},
})
mediaMenu.manualRow(createBackMainMenuButtons())
});
mediaMenu.manualRow(createBackMainMenuButtons());

menu.submenu('Media Menu', 'media', mediaMenu)
menu.submenu('Media Menu', 'media', mediaMenu);

const menuMiddleware = new MenuMiddleware('/', menu)
console.log(menuMiddleware.tree())
const menuMiddleware = new MenuMiddleware('/', menu);
console.log(menuMiddleware.tree());

const bot = new Bot(process.env['BOT_TOKEN'])
const bot = new Bot(process.env['BOT_TOKEN']);

bot.on('callback_query:data', async (ctx, next) => {
console.log(
'another callbackQuery happened',
ctx.callbackQuery.data.length,
ctx.callbackQuery.data,
)
return next()
})
);
return next();
});

bot.command('start', async ctx => menuMiddleware.replyToContext(ctx))
bot.use(menuMiddleware.middleware())
bot.command('start', async ctx => menuMiddleware.replyToContext(ctx));
bot.use(menuMiddleware.middleware());

bot.catch(error => {
console.log('bot error', error)
})
console.log('bot error', error);
});

async function startup() {
await bot.start({
onStart(botInfo) {
console.log(new Date(), 'Bot starts as', botInfo.username)
console.log(new Date(), 'Bot starts as', botInfo.username);
},
})
});
}

startup()
startup();
Loading

0 comments on commit 7c24bc5

Please sign in to comment.