85 lines
2.9 KiB
Python
Raw Permalink Normal View History

2022-01-07 03:24:12 +05:30
#!/usr/bin/env python3
2022-03-17 18:39:30 +05:30
2022-01-07 03:24:12 +05:30
#https://sudonull.com/post/62683-Telegram-bots-Uploading-files-larger-than-50mb
2022-01-09 04:19:22 +05:30
'''
2022-01-07 03:24:12 +05:30
fun facts i just came accross
2022-01-09 04:19:22 +05:30
the .send_file() function will have to send the file to the bot that will be serving the user,
2022-01-07 03:24:12 +05:30
Just uploading the file to the server via upload , getting file_id and passing it to the bot will not work,
2022-01-09 04:19:22 +05:30
file_id only works inside the chat in which it was created.
So that our bot can send the file to the user by file_id
the agent must send the bot this file
2022-01-07 03:24:12 +05:30
then the bot will receive own file_id for this file and will be able to dispose of it.
'''
from telethon import TelegramClient
from telethon.tl.types import DocumentAttributeVideo
import asyncio
import json
2022-01-15 19:56:53 +05:30
import logging
2022-01-07 03:24:12 +05:30
2022-03-17 18:39:30 +05:30
# basic logging
2022-01-15 19:56:53 +05:30
logging.basicConfig(format='%(levelname)s - %(asctime)s - %(name)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
2022-01-07 03:24:12 +05:30
2022-03-17 18:39:30 +05:30
2022-03-09 22:01:36 +05:30
try:
with open('uploaderService/config/agentConfig.json', 'w+') as config:
configdata = json.load(config)
2022-03-17 18:39:30 +05:30
except Exception as e:
2022-03-09 22:01:36 +05:30
raise Exception('CONFIG FILE NOT FOUND!')
2022-01-07 03:24:12 +05:30
entity = configdata.get('entity') #session name - it doesn't matter what
api_id = configdata.get('api_id')
api_hash = configdata.get('api_hash')
phone = configdata.get('phone')
bot_name = configdata.get('bot_name')
2022-01-15 19:56:53 +05:30
async def callback(current, total):
2022-01-09 04:19:22 +05:30
# for upload progression
2022-01-15 19:56:53 +05:30
logger.info('Uploaded: {:.2%}'.format(current / total))
2022-01-07 03:24:12 +05:30
2022-01-09 04:19:22 +05:30
'''
bot_name = the actual bot name
file_path = where the file is downloaded
2022-03-09 22:01:36 +05:30
chat_id = this is the end user chat_id, sent over caption to bot,
so it can parse and send it to the correct user
object_id = an internal id used for mapping of file_id
and filename stored in the server(for optimization).
2022-01-09 04:19:22 +05:30
'''
async def uploadVideo(bot_name,file_path,chat_id,object_id):
2022-03-09 22:01:36 +05:30
logger.info('video uploading initiated')
2022-01-07 03:24:12 +05:30
async with TelegramClient(entity, api_id, api_hash) as client:
if not await client.is_user_authorized():
2022-03-17 18:39:30 +05:30
#await client.send_code_request(phone)
'''at the first start - uncomment, after authorization to avoid FloodWait I advise you to comment'''
await client.sign_in(phone, input('Enter code: '))
2022-01-07 03:24:12 +05:30
await client.send_file(
str(bot_name),
file_path,
2022-01-09 04:19:22 +05:30
caption=str(chat_id + ':' + object_id),
2022-01-07 03:24:12 +05:30
attributes=[DocumentAttributeVideo(0,0,0)],
progress_callback=callback,
part_size_kb=512,
supports_streaming=True,
)
await client.disconnect()
return 0
async def main(argv):
file_path = argv[1]
2022-01-09 04:19:22 +05:30
chat_id = argv[2]
object_id = argv[3]
2022-01-07 03:24:12 +05:30
2022-01-09 04:19:22 +05:30
await uploadVideo(bot_name,file_path,chat_id,object_id)
2022-01-07 03:24:12 +05:30
if __name__ == '__main__':
import sys
asyncio.run(main(sys.argv[0:]))
2022-01-07 03:24:12 +05:30
2022-03-17 18:39:30 +05:30
# python uploader.py <file_path> <caption> <chat_id> <object_id>