Advertisement
ant1sipater

[IoT] Practical 7 - Telegram Bot

Sep 29th, 2023
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. import time, datetime
  2. import RPi.GPIO as GPIO
  3. import telepot
  4. from telepot.loop import MessageLoop
  5.  
  6. led = 26
  7. now = datetime.datetime.now()
  8. GPIO.setmode(GPIO.BCM)
  9. GPIO.setwarnings(False)
  10.  
  11. #LED
  12. GPIO.setup(led, GPIO.OUT)
  13. GPIO.output(led, 0) #Off initially
  14. def action(msg):
  15.     chat_id = msg['chat']['id']
  16.     command = msg['text']
  17.    
  18.     print('Received: %s' % command)
  19.    
  20.     if 'on' in command:
  21.         message = 'Turned on'
  22.         if 'led' in command:
  23.             message = message + ' led'
  24.             GPIO.output(led, 1)
  25.         telegram_bot.sendMessage(chat_id, message)
  26.        
  27.     if 'off' in command:
  28.         message = 'Turned off'
  29.         if 'led' in command:
  30.             message = message + ' led'
  31.             GPIO.output(led, 0)
  32.         telegram_bot.sendMessage(chat_id, message)
  33.  
  34. telegram_bot = telepot.Bot('6627132833:AAH3GtaajsAh8zTN9MvnWypZUyKWNJ5KntI')
  35. print(telegram_bot.getMe())
  36.  
  37. MessageLoop(telegram_bot, action).run_as_thread()
  38. print("Up and running...")
  39.  
  40. while 1:
  41.     time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement