Skip to content

Instantly share code, notes, and snippets.

@eyalcohen4
Created October 18, 2023 11:53
Show Gist options
  • Save eyalcohen4/b715a7676f5f50f1c3686524a0496fe2 to your computer and use it in GitHub Desktop.
Save eyalcohen4/b715a7676f5f50f1c3686524a0496fe2 to your computer and use it in GitHub Desktop.
telegram to aws s3
from telegram import Bot, Update
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
import boto3
import logging
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
# AWS configurations
AWS_ACCESS_KEY = 'YOUR_AWS_ACCESS_KEY'
AWS_SECRET_KEY = 'YOUR_AWS_SECRET_KEY'
BUCKET_NAME = 'YOUR_BUCKET_NAME'
# Initialize boto3 client for S3
s3_client = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY)
def start(update, context):
update.message.reply_text('Send me a file and I will upload it to S3!')
def handle_document(update, context):
file = context.bot.getFile(update.message.document.file_id)
file.download(update.message.document.file_name)
# Upload to S3
with open(update.message.document.file_name, 'rb') as f:
s3_client.upload_fileobj(f, BUCKET_NAME, update.message.document.file_name)
update.message.reply_text('File uploaded to S3!')
def main():
TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'
updater = Updater(token=TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(MessageHandler(Filters.document, handle_document))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment