Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import io
- import os
- from aiohttp import web
- from telethon import utils
- from telethon.sync import TelegramClient
- from telethon.tl.types import DocumentAttributeAudio
- bot = TelegramClient('bot', int(os.environ['TG_API_ID']), os.environ['TG_API_HASH']) \
- .start(bot_token=os.environ['TG_TOKEN'])
- async def send_audio(request):
- data = await request.post()
- if "chat_id" not in data:
- return web.Response(status=400,
- text='Bad Request: there is no chat_id in the request')
- if "audio" not in data:
- return web.Response(status=400, text='Bad Request: there is no audio in the request')
- attrs = DocumentAttributeAudio(0)
- if "duration" in data:
- attrs.duration = int(data['duration'])
- if "title" in data:
- attrs.title = data['title']
- if "performer" in data:
- attrs.performer = data['performer']
- f = data['audio']
- audio = io.BytesIO(f.file.read())
- audio.name = f.filename
- thumb = None
- if "thumb" in data:
- thumb = io.BytesIO(data['thumb'].file.read())
- thumb.name = 'thumb.jpg'
- try:
- res = await bot.send_file(int(data['chat_id']),
- file=audio,
- caption=data.get('caption'),
- parse_mode=data.get('parse_mode'),
- thumb=thumb,
- attributes=[attrs])
- except Exception as e:
- return web.Response(status=500, text=str(e))
- if res.media is None:
- return web.Response(status=500, text='Upload error')
- file_id = utils.pack_bot_file_id(res.media.document)
- if file_id is None:
- return web.Response(status=500, text='ID encoding error')
- return web.Response(text=file_id)
- app = web.Application(client_max_size=200*1024*1024)
- app.add_routes([web.post('/sendAudio', send_audio)])
- web.run_app(app)
Advertisement
Add Comment
Please, Sign In to add comment