Add Telegram bot support #23

Merged
Berack96 merged 23 commits from 6-telegram-interface into main 2025-10-13 10:49:46 +02:00
Showing only changes of commit 991686aa45 - Show all commits

View File

@@ -0,0 +1,59 @@
import os
from dotenv import load_dotenv
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, CommandHandler, CallbackQueryHandler, MessageHandler, filters, ContextTypes
# Esempio di funzione per gestire il comando /start
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not update.message: return
copilot-pull-request-reviewer[bot] commented 2025-10-12 20:27:14 +02:00 (Migrated from github.com)
Review

[nitpick] Consider using explicit None return for better code clarity: 'return None' instead of bare 'return'.

    if not update.message: return None
[nitpick] Consider using explicit None return for better code clarity: 'return None' instead of bare 'return'. ```suggestion if not update.message: return None ```
await update.message.reply_text('Ciao! Inviami un messaggio e ti risponderò!')
# Esempio di funzione per fare echo del messaggio ricevuto
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
message = update.message
if not message: return
copilot-pull-request-reviewer[bot] commented 2025-10-12 20:27:14 +02:00 (Migrated from github.com)
Review

[nitpick] Consider using explicit None return for better code clarity: 'return None' instead of bare 'return'.

    if not message: return None
[nitpick] Consider using explicit None return for better code clarity: 'return None' instead of bare 'return'. ```suggestion if not message: return None ```
print(f"Ricevuto messaggio: {message.text} da chat id: {message.chat.id}")
await message.reply_text(text=f"Hai detto: {message.text}")
# Esempio di funzione per far partire una inline keyboard (comando /keyboard)
async def inline_keyboard(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not update.message: return
copilot-pull-request-reviewer[bot] commented 2025-10-12 20:27:14 +02:00 (Migrated from github.com)
Review

[nitpick] Consider using explicit None return for better code clarity: 'return None' instead of bare 'return'.

    if not update.message: return None
[nitpick] Consider using explicit None return for better code clarity: 'return None' instead of bare 'return'. ```suggestion if not update.message: return None ```
keyboard = [
[
InlineKeyboardButton("Option 1", callback_data='1'),
InlineKeyboardButton("Option 2", callback_data='2'),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text('Please choose:', reply_markup=reply_markup)
async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
query = update.callback_query
if not query: return
copilot-pull-request-reviewer[bot] commented 2025-10-12 20:27:14 +02:00 (Migrated from github.com)
Review

[nitpick] Consider using explicit None return for better code clarity: 'return None' instead of bare 'return'.

    if not query: return None
[nitpick] Consider using explicit None return for better code clarity: 'return None' instead of bare 'return'. ```suggestion if not query: return None ```
await query.answer()
await query.edit_message_text(text=f"Selected option: {query.data}")
def main():
print("Bot in ascolto...")
load_dotenv()
token = os.getenv("TELEGRAM_BOT_TOKEN", '')
app = Application.builder().token(token).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("keyboard", inline_keyboard))
app.add_handler(MessageHandler(filters=filters.TEXT, callback=echo))
app.add_handler(CallbackQueryHandler(button_handler))
app.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()