Skip to content

Instantly share code, notes, and snippets.

@zenlor
Created March 17, 2023 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zenlor/a03b97f5c6003e31e35daf56a2a73f7e to your computer and use it in GitHub Desktop.
Save zenlor/a03b97f5c6003e31e35daf56a2a73f7e to your computer and use it in GitHub Desktop.
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import os
# Set up the Telegram bot
bot = telegram.Bot(token='YOUR_TELEGRAM_BOT_TOKEN')
updater = Updater(token='YOUR_TELEGRAM_BOT_TOKEN', use_context=True)
dispatcher = updater.dispatcher
# Set up the database
# Replace with your own database credentials
DATABASE_URL = os.environ['DATABASE_URL']
# Define a function to save images and videos
def save_media(update, context):
# Get the file ID from the message
file_id = update.message.photo[-1].file_id
# Get the file object from Telegram
file = context.bot.get_file(file_id)
# Save the file to the database
# Replace with your own database code
db = get_database_connection()
db.execute("INSERT INTO media (file_id) VALUES (?)", (file.file_id,))
db.commit()
db.close()
# Define a function to post images and videos
def post_media(update, context):
# Get the file ID from the message
file_id = update.message.photo[-1].file_id
# Get the file object from Telegram
file = context.bot.get_file(file_id)
# Post the file to the chat
context.bot.send_photo(chat_id=update.effective_chat.id, photo=file)
# Set up the handlers for the bot
save_handler = MessageHandler(Filters.photo, save_media)
post_handler = CommandHandler('post', post_media)
dispatcher.add_handler(save_handler)
dispatcher.add_handler(post_handler)
# Start the bot
updater.start_polling()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment