Skip to content

Instantly share code, notes, and snippets.

@alexeyev
Last active August 31, 2021 18:13
Show Gist options
  • Save alexeyev/e211f410890560e5ae0fb573b96c9adc to your computer and use it in GitHub Desktop.
Save alexeyev/e211f410890560e5ae0fb573b96c9adc to your computer and use it in GitHub Desktop.
Считаем ворон с помощью Raspberry Pi и Telegram API
# coding: utf-8
import configparser
import logging
import telebot
from time import sleep
from picamera import PiCamera
logger = logging.getLogger("counting-crops")
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler("everything.log")
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
parser = configparser.ConfigParser()
parser.read("configs/bot_config.ini")
ACCESS_KEY = parser["telegram"]["key"]
bot = telebot.TeleBot(ACCESS_KEY, num_threads=4)
content_types = "audio, sticker, video, video_note, location, contact, new_chat_members, " \
"left_chat_member, new_chat_title, new_chat_photo, delete_chat_photo, group_chat_created, " \
"supergroup_chat_created, channel_chat_created, migrate_to_chat_id, migrate_from_chat_id, " \
"pinned_message, document, photo, text".split(", ")
# NOTA BENE: there should be a queue
# with timeouts for each incoming request.
# Bot crashes on too many RPS to the camera.
camera = PiCamera()
def shoot(prefix: str, timeout_for_focus_seconds=5):
camera.start_preview()
sleep(timeout_for_focus_seconds)
camera.capture(prefix + ".jpg")
camera.stop_preview()
return open(prefix + ".jpg", "rb")
@bot.message_handler(commands=["start", "help"])
def send_welcome(message):
logger.debug(message)
bot.reply_to(message, "Привет, это бот, который докладывает обстановку, фотографируя то, что видит через камеру "
"Raspberry Pi. Ещё в процессе разработки. "
"Используйте команду /bang.")
@bot.message_handler(commands=["bang"])
def process_bang(message):
logger.debug(message)
username = message.from_user.username
date = message.date
prefix = "data/%d_%d_%s" % (date, message.message_id, username)
image = camera.shoot(prefix)
bot.send_photo(message.chat.id, image, caption="""¯\\_(ツ)_/¯""")
@bot.message_handler(content_types=["voice"])
def handle_voice(message):
logger.debug(message)
bot.reply_to(message, "Не очень удобно сейчас слушать голосовые, давайте лучше командами.")
@bot.message_handler(content_types=content_types)
def handle_other(message):
logger.debug(message)
bot.reply_to(message, "Увы, бот воспринимает только команды, начинающиеся на \"/\".")
bot.polling()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment