Add bot.py
This commit is contained in:
		
							
								
								
									
										36
									
								
								bot.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								bot.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
from telegram.ext import Updater, CommandHandler
 | 
			
		||||
import config
 | 
			
		||||
import requests
 | 
			
		||||
import json
 | 
			
		||||
import logging
 | 
			
		||||
from PIL import Image
 | 
			
		||||
from io import BytesIO
 | 
			
		||||
 | 
			
		||||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \
 | 
			
		||||
                    %(message)s', level=logging.INFO)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def cursed(bot, update, args):
 | 
			
		||||
    try:
 | 
			
		||||
        lim = args[0]
 | 
			
		||||
    except(IndexError):
 | 
			
		||||
        lim = 1
 | 
			
		||||
    url = f"https://www.reddit.com/r/cursedimages/top.json?limit={lim}"
 | 
			
		||||
    headers = {
 | 
			
		||||
            'user-agent': ('windows / chrome 67 [desktop]: mozilla/5.0 '
 | 
			
		||||
                           '(windows nt 10.0; win64; x64)')}
 | 
			
		||||
    res = json.loads(requests.get(url, headers=headers).text)
 | 
			
		||||
    image_url = res["data"]["children"][-1]["data"]["url"]
 | 
			
		||||
    image = requests.get(image_url, headers=headers)
 | 
			
		||||
    Image.open(BytesIO(image.content)).save('test.webp')
 | 
			
		||||
    chat_id = update.message.chat_id
 | 
			
		||||
    bot.send_sticker(chat_id, open('test.webp', 'rb'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
updater = Updater(token=config.api_key)
 | 
			
		||||
dispatcher = updater.dispatcher
 | 
			
		||||
 | 
			
		||||
dispatcher.add_handler(CommandHandler('cursed', cursed, pass_args=True))
 | 
			
		||||
updater.start_polling()
 | 
			
		||||
		Reference in New Issue
	
	Block a user