Skip to content

Instantly share code, notes, and snippets.

@jeremy5189
Created April 7, 2017 15:02
Show Gist options
  • Save jeremy5189/18a265a1037ca3c844e75396c4b53f23 to your computer and use it in GitHub Desktop.
Save jeremy5189/18a265a1037ca3c844e75396c4b53f23 to your computer and use it in GitHub Desktop.
# This is a simple echo bot using the decorator mechanism.
# It echoes any incoming text messages.
import subprocess
import commands
import telebot
from telebot import types
API_TOKEN = ''
bot = telebot.TeleBot(API_TOKEN)
def check_user(uid):
allow_user_id = ['116498922', '139407867']
allow = False
for user_id in allow_user_id:
if user_id == str(uid):
allow = True
break
return allow
def get_lan_ip():
return commands.getoutput("/sbin/ifconfig").split("\n")[1].split()[1][5:]
def bash_cmd(bashCommand):
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
# Init GPIO
bash_cmd('bash /home/pi/gpio-init.sh')
# Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('/open', '/ip')
bot.reply_to(message, "Hello! This is Jeremy Home Door Bot, Type /open to open the door", reply_markup=markup)
@bot.message_handler(commands=['ip'])
def send_ip(message):
ip = get_lan_ip()
print 'reply ip: ' + ip
bot.reply_to(message, ip)
# Handle all other messages with content_type 'text' (content_types defaults to ['text'])
@bot.message_handler(commands=['open'])
def open_door(message):
if check_user(message.from_user.id):
print 'Prepare to open door'
bash_cmd('bash /home/pi/gpio-toggle.sh')
bot.reply_to(message, 'Door opened')
else:
print str(message.from_user.id) + ' not allowd'
bot.reply_to(message, 'User ID not allowd')
# Loop
bot.polling()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment