initial commit

This commit is contained in:
vance 2022-10-26 00:52:28 -07:00
commit 4425b6a79f
3 changed files with 85 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/.idea
devenv.session

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# snicker
serve a random sticker from your Telegram account

81
snicker.py Executable file
View File

@ -0,0 +1,81 @@
import asyncio
import random
from PIL import Image
from io import BytesIO
from telethon import TelegramClient
from telethon.tl.functions.messages import GetAllStickersRequest, GetStickerSetRequest
from telethon.tl.types import InputStickerSetID
from aiohttp import web
loop = asyncio.get_event_loop()
api_id = 0 # Put you api id here
api_hash = '' # Put your api hash here
client = TelegramClient('devenv', api_id, api_hash, loop=loop)
async def get_sticker():
sticker_sets = await client(GetAllStickersRequest(0))
sticker_set = random.choice(sticker_sets.sets)
stickers = await client(
GetStickerSetRequest(
stickerset=InputStickerSetID(
id=sticker_set.id,
access_hash=sticker_set.access_hash
)
)
)
sticker = BytesIO()
await client.download_media(random.choice(stickers.documents), file=sticker)
return sticker
async def get_handler(request):
sticker = Image.open(
await get_sticker()
).convert('RGBA')
png = BytesIO()
sticker.save(png, 'png')
png.seek(0)
return web.Response(body=png.read(), content_type='image/png')
async def start_site(app, address='0.0.0.0', port=8080):
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, address, port)
await site.start()
return runner
async def main():
await client.start()
web_app = web.Application()
web_app.add_routes(
[
web.get('/', get_handler)
]
)
runner = await start_site(web_app)
try:
while True:
await asyncio.sleep(0.01)
except KeyboardInterrupt:
pass
await runner.cleanup()
if __name__ == '__main__':
try:
loop.run_until_complete(main())
except KeyboardInterrupt:
pass
loop.close()