implement bot keyboard with basics commands

This commit is contained in:
andrea
2025-07-26 11:00:20 +02:00
parent f360fe4200
commit 100bb4fe53
2 changed files with 67 additions and 17 deletions

80
bot.py
View File

@@ -1,13 +1,17 @@
#import datetime
#import pytz
from telegram.ext import Application, CommandHandler, MessageHandler, filters
from telegram.ext import Application
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler
from telegram.ext import filters
from telegram import ReplyKeyboardMarkup
from telegram import ReplyKeyboardRemove
import main as _brsim
import debug as _dbg
from bot_libs import player_handling as _bot_player
from bot_libs import syms as _botsyms
async def loop_game(context):
chat_id = context.job.chat_id
async def simulate_day(context, chat_id):
if 'arena' in context.application.bot_data:
Arena= context.application.bot_data['arena']
if len(Arena.get_alive_players()) <= 1: return # TODO break the loop
@@ -19,21 +23,29 @@ async def loop_game(context):
print('Arena non trovata')
await context.bot.send_message(chat_id, 'Che e\' successo? un Guarino ha rubato l\'arena, avvia una nuova partita con /start')
async def loop_game(context):
chat_id = context.job.chat_id
return await simulate_day(context, chat_id)
async def bot_start(update, context):
await update.message.reply_text(_botsyms.START_MSG)
keyboard = [
['Init/Restart'],
['Add Player'],
['Get Players', 'Get Alive Players', 'Get Death Players'],
['Simulate Day', 'Run Periodically']
]
reply_markup= ReplyKeyboardMarkup(keyboard, one_time_keyboard=False, resize_keyboard=True)
chat_id = update.effective_chat.id
print(f'{chat_id}: Sto costruendo il mondo di gioco...')
Arena= _brsim.init_arena()
await update.message.reply_text('Ho creato il mondo di gioco')
await update.message.reply_text('Ho creato il mondo di gioco', reply_markup=reply_markup)
context.application.bot_data['arena'] = Arena
async def echo(update, context):
testo_ricevuto = update.message.text
await update.message.reply_text(_botsyms.WIP_MSG)
async def start_game(update, context):
async def start_loop_game(update, context):
chat_id = update.effective_chat.id
if 'arena' not in context.application.bot_data:
print(f'{chat_id}: Arena non trovata')
@@ -56,19 +68,57 @@ async def start_game(update, context):
#)
print(f'Job giornaliero creato per la chat {chat_id}')
async def bot_commands(update, context):
text= update.message.text
chat_id = update.effective_chat.id
if text == 'Init/Restart':
print(f'bot_command: {chat_id} Init/Restart')
return await bot_start(update, context)
if text == 'Add Player':
print(f'bot_command: {chat_id} Add Player')
context.application.bot_data['ask_name'] = 1
return
if text == 'Get Players':
print(f'bot_command: {chat_id} Get Players')
return await _bot_player.get_players(update, context)
if text == 'Get Alive Players':
print(f'bot_command: {chat_id} Get Alive Players')
return await _bot_player.get_alive_players(update, context)
if text == 'Get Death Players':
print(f'bot_command: {chat_id} Get Death Players')
return await _bot_player.get_death_players(update, context)
if text == 'Simulate Day':
print(f'bot_command: {chat_id} Simulate Day')
return await simulate_day(context, chat_id)
if text == 'Run Periodically':
print(f'bot_command: {chat_id} Run Periodically')
return await update.message.reply_text(_botsyms.WIP_MSG)
#return await simulate_day(context, chat_id)
waiting_for_name= context.application.bot_data.get('ask_name')
if waiting_for_name:
print(f'bot_command: {chat_id} Collected Player Name {text}')
del(context.application.bot_data['ask_name'])
return await _bot_player.add_player(update, context, text)
print(f'{chat_id} ha inviato questo testo: {text}')
await update.message.reply_text(_botsyms.WIP_MSG)
def main():
application = Application.builder().token(_botsyms.TOKEN).build()
application.add_handler(CommandHandler('start', bot_start))
application.add_handler(CommandHandler('start_game', start_game))
#application.add_handler(CommandHandler('start_game', start_loop_game))
# player handler
application.add_handler(CommandHandler('add_player', _bot_player.add_player))
application.add_handler(CommandHandler('get_players', _bot_player.get_players))
application.add_handler(CommandHandler('get_alive_players', _bot_player.get_alive_players))
application.add_handler(CommandHandler('get_death_players', _bot_player.get_death_players))
#application.add_handler(CommandHandler('add_player', _bot_player.add_player))
#application.add_handler(CommandHandler('get_players', _bot_player.get_players))
#application.add_handler(CommandHandler('get_alive_players', _bot_player.get_alive_players))
#application.add_handler(CommandHandler('get_death_players', _bot_player.get_death_players))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, bot_commands))
print('Bot in esecuzione...')
application.run_polling()