Advertisement
Guest User

this kind of code

a guest
Jun 26th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. import logging
  2.  
  3. from telegram import Update, ForceReply
  4. from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
  5.  
  6. from gpiozero import CPUTemperature
  7. import re
  8. import subprocess
  9. import json
  10.  
  11. from responsetexts import ResponseTexts
  12. from soundModule import SoundControl
  13. from temperatureBot.TemperatureBot import TemperatureBot
  14.  
  15. # Enable logging
  16. logging.basicConfig(
  17.     format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
  18. )
  19.  
  20. logger = logging.getLogger(__name__)
  21.  
  22. try:
  23.     with open('config.json', 'r') as f:
  24.         data=f.read()
  25.     # parse file
  26.     configFileData = json.loads(data)
  27.     myChatId = configFileData["chatId"]
  28.     myBotToken = configFileData["botToken"]
  29.     myBotNickName = configFileData["botName"]["nickName"]
  30.     myBotFullName = configFileData["botName"]["fullName"]
  31.  
  32. except Exception as e:
  33.     print('Exception reading config.json file:', e)
  34.  
  35.  
  36. # Define a few command handlers. These usually take the two arguments update and
  37. # context.
  38. def getCurrentTemperature():
  39.     return CPUTemperature().temperature
  40.  
  41. def getTemperature(update: Update, context: CallbackContext) -> None:
  42.     """when the command /temperature is issued."""
  43.    
  44. def adjustSound(update: Update, context: CallbackContext) -> None:
  45.     """when the command /sound is issued."""
  46.  
  47. def shutdown(update: Update, context: CallbackContext) -> None:
  48.     """when the command /shutdown is issued."""
  49.    
  50. def getIPHandler(update: Update, context: CallbackContext) -> None:
  51.     """when command /ip is issued."""
  52.    
  53. def normalTextHandler(update: Update, context: CallbackContext) -> None:
  54.     """Handle the user messages which are non-commands."""
  55.  
  56. def help_command(update: Update, context: CallbackContext) -> None:
  57.     """when command /help is issued."""
  58.  
  59. def main() -> None:
  60.     TemperatureBot(myChatId, myBotToken, 60, 300, myBotNickName).temperaturePolling()
  61.     """Start the bot."""
  62.     # Create the Updater and pass it your bot's token.
  63.     updater = Updater(myBotToken)
  64.     # Get the dispatcher to register handlers
  65.     dispatcher = updater.dispatcher
  66.  
  67.     # on different commands - answer in Telegram
  68.     dispatcher.add_handler(CommandHandler("temperature", getTemperature,Filters.chat(myChatId)))
  69.     dispatcher.add_handler(CommandHandler("sound", adjustSound,Filters.chat(myChatId), pass_args=True))
  70.     dispatcher.add_handler(CommandHandler("shutdown", shutdown,Filters.chat(myChatId)))
  71.     dispatcher.add_handler(CommandHandler("ip", getIPHandler,Filters.chat(myChatId)))
  72.     dispatcher.add_handler(CommandHandler("help", help_command,Filters.chat(myChatId)))
  73.  
  74.     # on non command i.e message - echo the message on Telegram
  75.     dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, normalTextHandler))
  76.  
  77.     # Start the Bot
  78.     updater.start_polling()
  79.  
  80.     # Run the bot until you press Ctrl-C or the process receives SIGINT,
  81.     # SIGTERM or SIGABRT. This should be used most of the time, since
  82.     # start_polling() is non-blocking and will stop the bot gracefully.
  83.     updater.idle()
  84.  
  85.  
  86. if __name__ == '__main__':
  87.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement