Closed viniolivieri closed 4 years ago
The first (unrelated) thing that i saw: you should not use ctx.from.first_name
as something unique. Maybe its only used by your backend to have some readable player1 player2 names, then its fine. But think about two people with the same name playing a game together. It should not break your backend logic ;)
If you want to use something unique go for ctx.from.id
.
Back to your original question. I'll abstract your api calls with methods:
async function newGame() {return id}
async function enterGame(gameId) {…}
// I would expect your api to also have something like this:
async function listJoinableGames() {return [id1, id2, …]}
async function listMyGames(userId) {return id1, id2, …]}
async function gameDetails(gameId) {…}
With these methods you can create a menu which allows you to either create a new game or join one of the existing ones.
const menu = new TelegrafInlineMenu('Join or create a game')
menu.button('Create game', 'create', {
doFunc: async ctx => {
await newGame()
}
})
menu.select('join', () => listJoinableGames(), {
setFunc: async (ctx, key) => {
await enterGame(key)
}
})
And you can create a menu showing all the current games a player is in with submenus specific to the game the user wants to view:
const overviewMenu = new TelegrafInlineMenu('all your games')
const gameMenu = new TelegrafInlineMenu(async ctx => gameDetails(ctx.match[1]))
overviewMenu.selectSubmenu('game', () => listMyGames(ctx.from.id), gameMenu)
A while ago i did something similar as a proof of concept with a blockchain based Tic Tac Toe. As I abstracted the api calls into methods like enterGame()
its very similar to what you want to achieve just with a different implementation of enterGame()
(http instead of blockchain stuff)
Take a look here: https://github.com/EdJoPaTo/rell-tik-tac-toe-telegram-bot/blob/master/source/bot-parts/menu/index.ts
I hope it helps! Feel free to ask questions :)
I'll just close this due to inactivity. If you have any questions feel free to ask ahead!
Hey! First of all, very nice work! And now the question: I am building a project that is for self training purposes, a card-game that is managed by an API and the playable part want want to do as a Bot of telegram.
The API generates an unique_id that is needed for querying the tables and managing the current game. The id is generated for example posting at
/new_game
, then I need to enter the current game or current session at/{unique_id}/enter_game.
I need a variable that can go over buttons and make API calls with it, do you think it is possible? I am trying this and it is not working.