Skip to content

Instantly share code, notes, and snippets.

@mbaneshi
Created June 20, 2023 08:10
Show Gist options
  • Save mbaneshi/fbf2263d03b217c895625071d4657e88 to your computer and use it in GitHub Desktop.
Save mbaneshi/fbf2263d03b217c895625071d4657e88 to your computer and use it in GitHub Desktop.
work in progress
# import os
# import requests
# from flask import Flask, request
from flask import Flask
from telegram import Bot
# from dotenv import load_dotenv
# Load environment variables from .env file in the current working directory
# load_dotenv()
# Get the Telegram bot token from the environment variable
# bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
bot_token = '6295326366:AAELq52zhHbskOOgg9kt27qT_Zn1OGs89xA'
# Get the webhook URL from the environment variable
# webhook_url = os.getenv('WEBHOOK_URL')
# webhook_url = os.getenv('WEBHOOK_URL')
WEBHOOK_URL = 'iZmp7I32YGSQG0FAWu2MhN5gg6cPoZ5e'
webhook_url = WEBHOOK_URL
# Create a bot instance
bot = Bot(token=bot_token)
# Create a Flask application instance
app = Flask(__name__)
@app.route('/iZmp7I32YGSQG0FAWu2MhN5gg6cPoZ5e', methods=['POST'])
def handle_webhook():
# Handle the incoming webhook request
# You can access the received data using `request.json` or `request.form`
# Process the received data and prepare a response
response_message = 'Received a webhook request'
# Return a response to the sender
return response_message, 200
@app.route('/set_webhook', methods=['GET'])
def set_webhook():
if not bot.get_webhook_info().url and webhook_url:
# Set the webhook URL if it is not already set and provided
bot.set_webhook(webhook_url)
return f'Webhook set to {webhook_url}'
else:
return 'Webhook URL is already set or not provided in .env file'
if __name__ == '__main__':
# Check if the webhook is already set
if not bot.get_webhook_info().url:
# Set the webhook if it is not already set
if webhook_url:
print(set_webhook())
else:
print('Webhook URL not set in .env file')
else:
print('Webhook URL is already set')
# Start the Flask application
app.run(host='0.0.0.0', port=8443)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment