Skip to content

Commit

Permalink
Update ToEnWikipediaBot.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jnton authored Apr 14, 2024
1 parent 3cff837 commit 57ca515
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions ToEnWikipediaBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,29 +196,38 @@ async def source(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text(
"You can find my source code here: https://github.com/JnTon/English-Wikipedia-Link-Converter-Telegram-Bot\n\nFeel free to contribute or fork to create your own version!"
)

# Define the Lambda handler
def lambda_handler(event, context):
# Set up the bot with your token
application = Application.builder().token("YOUR_TELEGRAM_BOT_TOKEN").build()

# Handlers
source_handler = CommandHandler('source', source)
wiki_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), check_wiki_link)
inline_handler = InlineQueryHandler(inline_query)

# Add handlers to the application
application.add_handler(source_handler)
application.add_handler(wiki_handler)
application.add_handler(inline_handler)

# This will start the bot and keep it running inside the Lambda environment
asyncio.run(application.run_polling())

# Return a simple HTTP response
return {
'statusCode': 200,
'body': json.dumps('Hello from your Telegram bot!')
}
try:
# Retrieve the token from environment variables
token = os.getenv("TELEGRAM_BOT_TOKEN")
if not token:
logger.error("Telegram bot token not found. Please set it in the Lambda environment variables.")
raise ValueError("Telegram bot token not found")

# Set up the Telegram bot application
application = Application.builder().token(token).build()

# Define and add handlers to the application
setup_handlers(application)

# Process the incoming event (Telegram update)
update = Update.de_json(json.loads(event['body']), application.bot)
application.process_update(update)

# Return a simple HTTP response
return {
'statusCode': 200,
'body': json.dumps('Hello from your Telegram bot!')
}

except Exception as e:
logger.error(f"An error occurred: {e}")
return {
'statusCode': 500,
'body': json.dumps('An error occurred')
}

# Ensure this part is not executed when the script is imported as a module in Lambda
#if __name__ == '__main__':
Expand Down

0 comments on commit 57ca515

Please sign in to comment.