all repos — groupgardenbot @ 056f4f5f06e566ab73cc13313ff23211fc3949f1

An extension of the game "botany", originally designed for unix-based systems, to the Telegram Bot API.

main.py (view raw)

  1import os, logging
  2logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
  3from telegram import Update
  4from telegram.ext import Updater, CallbackContext, CallbackQueryHandler, CommandHandler, MessageHandler, PicklePersistence
  5from dotenv import load_dotenv
  6load_dotenv()
  7from Gardening import Plant, get_plant_info
  8
  9def reply(update: Update, context: CallbackContext, text: str = "", markup: str = ""):
 10    return context.bot.send_message(chat_id=update.effective_chat.id, text=text, reply_markup=markup, parse_mode='Markdown')
 11
 12def get_plant(update: Update, context: CallbackContext):
 13        try:
 14            plant = context.bot_data[update.effective_user.id]["plant"]
 15        except KeyError:
 16            return None
 17        
 18        if plant is None: # probably useless but heh
 19            print("############### This wasn't so useless after all! ###############")
 20            return None
 21        
 22        plant.update()
 23        return plant
 24
 25def start(update: Update, context: CallbackContext):
 26    plant = get_plant(update, context)
 27    new = False
 28    
 29    if plant is None:
 30        context.bot_data[update.effective_user.id] = { "plant" : Plant(update.effective_user.id) }
 31        new = True
 32
 33    if plant.dead or plant.stage == 5:
 34        plant.start_over()
 35        new = True
 36    
 37    if new:
 38        return reply(update, context, "Hai piantato un nuovo seme! Adesso usa /water per innaffiarlo.")
 39    
 40    return reply(update, context, "La tua pianta non è ancora pronta per andarsene!")
 41
 42def water(update: Update, context: CallbackContext):
 43    plant = get_plant(update, context)
 44    
 45    if plant is None:
 46        return reply(update, context, "Non hai nessuna pianta da innaffiare! Usa /start per piantarne una.")
 47
 48    if plant.dead:
 49        return reply(update, context, "La tua pianta è morta... Usa /harvest per piantarne un'altra.")
 50    
 51    plant.water()
 52    return reply(update, context, "Pianta innaffiata.")
 53
 54def show(update: Update, context: CallbackContext):
 55    plant = get_plant(update, context)
 56    
 57    if plant is None:
 58        return reply(update, context, "Non hai nessuna pianta da mostrare! Usa /start per piantarne una.")
 59
 60    return reply(update, context, get_plant_info(plant))
 61
 62
 63'''
 64def keyboard_handler(update: Update, context: CallbackContext):
 65    query = update.callback_query
 66    data = query.data
 67    
 68    if data.startswith("reroll"):
 69        amount = int(data.split(" ")[1])
 70        
 71        if amount <= 1:
 72            return spin(update, context)
 73        return autospin(context, update.effective_chat.id, amount)
 74    
 75    match data:
 76        case "none":
 77            return query.answer("This button doesn't do anything.", context))
 78        case other:
 79            logging.error(f"unknown callback: {data}")
 80    
 81    return query.answer()
 82
 83def unknown(update: Update, context: CallbackContext):
 84    logging.info(f"User {update.message.from_user.full_name} sent {update.message.text_markdown_v2} and I don't know what that means.")
 85'''
 86def main():
 87    
 88    updater = Updater(token=os.getenv("token"),
 89                      persistence=PicklePersistence(filename='bot-data.pkl',
 90                                                    store_user_data=False,
 91                                                    store_bot_data=True,
 92                                                    store_callback_data=False,
 93                                                    store_chat_data=False
 94                                                    ))
 95    
 96    dispatcher = updater.dispatcher
 97    
 98    
 99    # commands
100    dispatcher.add_handler(CommandHandler('start', start))
101    dispatcher.add_handler(CommandHandler('water', water))
102    dispatcher.add_handler(CommandHandler('show', show))
103    
104    '''
105    # slot
106    dispatcher.add_handler(CommandHandler('spin', spin))
107    dispatcher.add_handler(CommandHandler('bet', bet))
108    dispatcher.add_handler(CommandHandler('cash', cash))
109    '''
110    
111    '''
112    dispatcher.add_handler(CallbackQueryHandler(callback=keyboard_handler))
113    dispatcher.add_handler(MessageHandler(Filters.command, unknown))
114    '''
115    
116    updater.start_polling()
117    print(os.getenv("bot_name"))
118    updater.idle()
119
120if __name__ == "__main__":
121    main()