Last active
December 6, 2016 12:01
-
-
Save bvanrijn/700492f0089adc0d98c1d2a7c346973a to your computer and use it in GitHub Desktop.
blink(1) telegram bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import tempfile | |
import logging | |
import requests | |
from colorthief import ColorThief | |
from blink1.blink1 import Blink1 | |
from telegram.ext import Updater, MessageHandler, Filters | |
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
b1 = Blink1() | |
updater = Updater(token='id:blah') | |
dispatcher = updater.dispatcher | |
def error(bot, update, error): | |
logger.warn('Update "%s" caused error "%s"' % (update, error)) | |
def color(bot, update): | |
color = update.message.text | |
if color == 'red': | |
b1.fade_to_rgb(1000, 255, 0, 0) | |
elif color == 'green': | |
b1.fade_to_rgb(1000, 0, 255, 0) | |
elif color == 'blue': | |
b1.fade_to_rgb(1000, 0, 0, 255) | |
elif color.startswith('#'): | |
color = color.strip('#') | |
r = int(color[:2], 16) | |
g = int(color[2:4], 16) | |
b = int(color[4:6], 16) | |
b1.fade_to_rgb(1000, r, g, b) | |
elif color.startswith('http'): | |
color_image(bot, update, img=color) | |
else: | |
b1.fade_to_rgb(3000, 255, 0, 0) | |
time.sleep(3) | |
b1.off() | |
def color_image(bot, update, img=None): | |
tmp = tempfile.TemporaryFile(mode='wb+') | |
if img == None: | |
file_id = update.message.photo[1].file_id | |
file_path = bot.getFile(file_id) | |
img = file_path.file_path | |
tmp.write(requests.get(img).content) | |
color_thief = ColorThief(tmp) | |
rgb = color_thief.get_color(quality=1) | |
r = rgb[0] | |
g = rgb[1] | |
b = rgb[2] | |
b1.fade_to_rgb(1000, r, g, b) | |
time.sleep(3) | |
b1.off() | |
tmp.close() | |
color_handler = MessageHandler(Filters.text, color) | |
image_handler = MessageHandler(Filters.photo, color_image) | |
dispatcher.add_handler(color_handler) | |
dispatcher.add_handler(image_handler) | |
dispatcher.add_error_handler(error) | |
updater.start_polling() | |
updater.idle() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment