Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- from telegram import Update, ForceReply
- from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
- from gpiozero import CPUTemperature
- import re
- import subprocess
- import json
- from responsetexts import ResponseTexts
- from soundModule import SoundControl
- from temperatureBot.TemperatureBot import TemperatureBot
- # Enable logging
- logging.basicConfig(
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
- )
- logger = logging.getLogger(__name__)
- try:
- with open('config.json', 'r') as f:
- data=f.read()
- # parse file
- configFileData = json.loads(data)
- myChatId = configFileData["chatId"]
- myBotToken = configFileData["botToken"]
- myBotNickName = configFileData["botName"]["nickName"]
- myBotFullName = configFileData["botName"]["fullName"]
- except Exception as e:
- print('Exception reading config.json file:', e)
- # Define a few command handlers. These usually take the two arguments update and
- # context.
- def getCurrentTemperature():
- return CPUTemperature().temperature
- def getTemperature(update: Update, context: CallbackContext) -> None:
- """when the command /temperature is issued."""
- def adjustSound(update: Update, context: CallbackContext) -> None:
- """when the command /sound is issued."""
- def shutdown(update: Update, context: CallbackContext) -> None:
- """when the command /shutdown is issued."""
- def getIPHandler(update: Update, context: CallbackContext) -> None:
- """when command /ip is issued."""
- def normalTextHandler(update: Update, context: CallbackContext) -> None:
- """Handle the user messages which are non-commands."""
- def help_command(update: Update, context: CallbackContext) -> None:
- """when command /help is issued."""
- def main() -> None:
- TemperatureBot(myChatId, myBotToken, 60, 300, myBotNickName).temperaturePolling()
- """Start the bot."""
- # Create the Updater and pass it your bot's token.
- updater = Updater(myBotToken)
- # Get the dispatcher to register handlers
- dispatcher = updater.dispatcher
- # on different commands - answer in Telegram
- dispatcher.add_handler(CommandHandler("temperature", getTemperature,Filters.chat(myChatId)))
- dispatcher.add_handler(CommandHandler("sound", adjustSound,Filters.chat(myChatId), pass_args=True))
- dispatcher.add_handler(CommandHandler("shutdown", shutdown,Filters.chat(myChatId)))
- dispatcher.add_handler(CommandHandler("ip", getIPHandler,Filters.chat(myChatId)))
- dispatcher.add_handler(CommandHandler("help", help_command,Filters.chat(myChatId)))
- # on non command i.e message - echo the message on Telegram
- dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, normalTextHandler))
- # Start the Bot
- updater.start_polling()
- # Run the bot until you press Ctrl-C or the process receives SIGINT,
- # SIGTERM or SIGABRT. This should be used most of the time, since
- # start_polling() is non-blocking and will stop the bot gracefully.
- updater.idle()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement