/
Py-To4Ka
/
SmartHome_WithTelegram
Обзор
Документация
Войти
/
Py-To4Ka
/
SmartHome_WithTelegram
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.py
40 строк
2 KB
Дмитрий
create main.py
17 янв 2025, 11:27
17 янв 2025, 11:27
cef0c17
Код
Авторство
О чём код?
from telegram.ext import Updater, CommandHandler import requests # Функция для управления светом def control_light(update, context): command = ' '.join(context.args) if command.lower() == 'on': # Здесь должен быть запрос к API умного дома для включения света response = requests.post("http://smart-home-api/light/on") update.message.reply_text("Свет включен!") elif command.lower() == 'off': # выключение света response = requests.post("http://smart-home-api/light/off") update.message.reply_text("Свет выключен!") else: update.message.reply_text("Используйте команду /light on или /light off") # Функция для контроля температуры def control_temperature(update, context): try: temperature = int(context.args[0]) # Здесь должен быть запрос к API умного дома для установки температуры response = requests.post(f"http://smart-home-api/thetmostat/set", json={"temperature": temperature}) update.message.reply_text(f"Температура установлена на {temperature}`C") except (IndexError, ValueError): update.message.reply_text("Используйте команду /temp <тумпература>") # Основная функция для запуска бота def main(): updater = Updater("Ваш_Telegram_API_токен", use_context=True) dp = updater.dispatcher dp.add_handler(CommandHandler("light", control_light)) dp.add_handler(CommandHandler("temp", control_temperature)) updater.start_polling() updater.idle() if __name__ == '__main__': main()