diff --git a/code/TelegramBot/TelegramBot.py b/code/TelegramBot/TelegramBot.py new file mode 100644 index 0000000..f1a3812 --- /dev/null +++ b/code/TelegramBot/TelegramBot.py @@ -0,0 +1,132 @@ +import telebot +import urllib3 + + +TOKEN_BOT = "xxxxxxxxx" #Telegram Token Bot +APPID_IBOARDBOT = "xxxxxxxxx" #iBoardbot APPID + + +commands = { # command description used in the "help" command + 'start': 'Get used to the bot', + 'help': 'Gives you information about the available commands', + 'text' : 'Send Message to iBoardbot', + 'textclear' : 'Clear board and send message to iBoardbot', + 'clear': 'Clear iBoardbot' +} + +knownUsers = [] # todo: save these in a file, +userStep = {} # so they won't reset every time the bot restarts + +def iboardbot_send(mode, message): + + + http = urllib3.PoolManager() + + if( mode == 1): #mode send text + mode = "text" + elif (mode ==2): #mode clear board and send text + mode = "message" + + urltext = "http://ibbapp.jjrobots.com/api/v1/"+mode+".php?APPID="+APPID_IBOARDBOT+"&TEXT=" + url = urltext+message + r = http.request('GET', url) + + +def iboardbot_clear(): #clear board + + http = urllib3.PoolManager() + + url = "http://ibbapp.jjrobots.com/api/v1/clear.php?APPID="+APPID_IBOARDBOT + r = http.request('GET', url) + + +# only used for console output now +#def listener(messages): + # for m in messages: + # if m.content_type == 'text': + # print the sent message to the console + #print str(m.chat.first_name).encode('utf-8') + " [" + str(m.chat.id).encode('utf-8') + "]: " + m.text + + +bot = telebot.TeleBot(TOKEN_BOT) + + +# handle the "/start" command +@bot.message_handler(commands=['start']) +def command_start(m): + cid = m.chat.id + if cid not in knownUsers: # if user hasn't used the "/start" command yet: + knownUsers.append(cid) # save user id, so you could brodcast messages to all users of this bot later + userStep[cid] = 0 # save user id and his current "command level", so he can use the "/getImage" command + bot.send_message(cid, "Hello, stranger, let me scan you...") + msg = "Scanning complete, I know you now. Chatid: " + str(cid) + bot.send_message(cid, msg) + command_help(m) # show the new user the help page + else: + bot.send_message(cid, "I already know you, no need for me to scan you again!") + + +# help page +@bot.message_handler(commands=['help']) +def command_help(m): + + cid = m.chat.id + help_text = "The following commands are available: \n" + for key in commands: # generate help text out of the commands dictionary defined at the top + help_text += "/" + key + ": " + help_text += commands[key] + "\n" + bot.send_message(cid, help_text) # send the generated help page + + +#Send Message to iBoardbot +@bot.message_handler(commands=['text']) +def command_text(m): + + space = "%20" + cid = m.chat.id + text = m.text.split() + + num_text = len(text) + + message = space.join(text) + + if num_text > 2: + message = space.join(text[1:]) + else: + message = text[1] + + iboardbot_send(1,message) + bot.send_message(cid, "Message sent correctly") + + +#Clear board and Send Message to iBoardbot +@bot.message_handler(commands=['textclear']) +def command_textclear(m): + + space = "%20" + cid = m.chat.id + text = m.text.split() + + num_text = len(text) + + message = space.join(text) + + if num_text > 2: + message = space.join(text[1:]) + else: + message = text[1] + + iboardbot_send(2,message) + bot.send_message(cid, "Message sent correctly") + + +#Clear iBoardbot +@bot.message_handler(commands=['clear']) +def command_clear(m): + cid = m.chat.id + iboardbot_clear() + bot.send_message(cid, "Command sent correctly") + + + +bot.polling(none_stop=True) diff --git a/code/TelegramBot/__init__.py b/code/TelegramBot/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/TelegramBot/instructions.md b/code/TelegramBot/instructions.md new file mode 100644 index 0000000..1361131 --- /dev/null +++ b/code/TelegramBot/instructions.md @@ -0,0 +1,62 @@ +Telegram for iBoardbot! +=================== + +Manage your iBoardbot from [Telegram](https://telegram.org/) with simple commands. Fun for all the family! + +---------- + + +Instructions +------------- + +> **Create [Telegram Bot](https://core.telegram.org/bots):** + +> 1. Open Telegram +> 2. Search [@BotFather](https://telegram.me/BotFather) +> 3. Start a conversation and write "/newbot" +> 4. Follow the steps to assign the name of the bot +> 5. Saves the token in a safe place +> 6. Talking to the BotFather you can change the settings anytime +> +> If you want to see your bot commands, you must configure it in the BotFather: +>> 7. Open @BotFather +>> 8. Write "/setcommands" and copy: + start - Start + help - Gives you information about the available commands + text - Send Message to iBoardbot + textclear - Clear board and Send Message to iBoardbot + clear - Clear iBoardbot + + +Installation +------------------- +In Linux: +> sudo apt-get install python-pip python-dev build-essential +> sudo pip install --upgrade pip +> sudo pip install --upgrade -r requirements.txt + +Create Daemon +------------------- +Run the program always in your system +>sudo apt-get install -y supervisor +>sudo service supervisor start +>cd /etc/supervisor/conf.d/ +>sudo nano iboardbot-tg.conf +>Copy and save: +>>[program:iboardbot-tg] + command=/{{YOUR DIRECTORY}}/TelegramBot.py + autostart=true + autorestart=true + startretries=3 + stderr_logfile=/var/log/iboardbot-tg.err.log + stdout_logfile=/var/log/iboardbot-tg.out.log + user=www-data + +>supervisorctl reread +supervisorctl update +If your process is not running, try explicitly telling Supervisord to start process via "supervisorctl start iboardbot-tg" + +More info about Supervisor: [Tutorial](https://stackedit.io/) + +### Thanks! + diff --git a/code/TelegramBot/requirements.txt b/code/TelegramBot/requirements.txt new file mode 100644 index 0000000..75dc45f --- /dev/null +++ b/code/TelegramBot/requirements.txt @@ -0,0 +1,5 @@ +urllib3 +pyTelegramBotAPI +pyopenssl +ndg-httpsclient +pyasn1 \ No newline at end of file