I still know very little about . Actually the question:

I write Telegram-bot. Purpose - The bot should ask what command I want to give him and execute it. So far, he has given him 5 of the most necessary commands.

Namely: There is a Raspberry Pi , from it I run the script itself, while I did it through the Web application, I control the Led diode connected to the raspberry. There are features

  • Turn on green light
  • Turn off the green light
  • Turn on the orange light
  • Turn off orange light
  • Led reload function.

Everything works through the web application. I use Telepot library. The task is to get rid of the Web and go to control from the bot. I also wrote code that works with the help of a bot, i.e. I can process and execute these functions from a bot. I want to do so in order not to enter all the commands with my hands, and that there would be convenient buttons.

Now made it so that when you start, he asks "What do you want to do?" And there are 5 buttons with these same functions. But everything "crashes" as soon as I press one of the buttons. Buttons also learned to do using the Telebot library. Tell me, how can they make friends? (Use simultaneously)

I attach the code:

 import subprocess import os import sys import time import random from datetime import datetime from datetime import timedelta import datetime as dt from functools import partial import telebot from telebot import types import RPi.GPIO as GPIO import telepot TOKEN = 'Мой токен.' bot = telebot.TeleBot(TOKEN) #LED LedG=22 LedO=17 GPIO.setmode(GPIO.BCM) GPIO.setup(LedG, GPIO.OUT) GPIO.setup(LedO, GPIO.OUT) def handle(msg): chat_id = msg['chat']['id'] command = msg['text'] print 'Got command: %s' % command @bot.message_handler(commands=['start']) def start(m): keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True) keyboard.add(*[types.KeyboardButton(name) for name in ['Turn On Green Led', 'Turn Off Green Led', 'Turn On Orange Led', 'Turn Off Orange Led', 'Reload']]) msg = bot.send_message(m.chat.id, 'What will we do with the drunken sailor?', reply_markup=keyboard) bot.register_next_step_handler(msg, name) def name(m): if m.text == 'Turn On Green Led': print "Turn On Green Led" bot.send_message(m.chat.id, 'Turn On Green Led', parse_mode='Markdown') GPIO.output(LedG,1) elif m.text == 'Turn Off Green Led': print "Turn Off Green Led" bot.send_message(m.chat.id, 'Turn Off Green Led', parse_mode='Markdown') GPIO.output(LedG,0) elif m.text == 'Turn On Orange Led': print "Turn On Orange Led" bot.send_message(m.chat.id, 'Turn On Orange Led', parse_mode='Markdown') GPIO.output(LedO,1) elif m.text == 'Turn Off Orange Led': print "Turn Off Orange Led" bot.send_message(m.chat.id, 'Turn Off Orange Led', parse_mode='Markdown') GPIO.output(LedO,0) elif m.text == 'Reload': print "RELOADING..." bot.send_message(m.chat.id, 'Led`s go to reload...', parse_mode='Markdown') GPIO.output(LedO,1) time.sleep(4) GPIO.output(LedO,0) bot.polling() while 1: time.sleep(10) 

Here is what happens from the moment the script runs to the end:

 /usr/local/lib/python2.7/dist-packages/urllib3/contrib/socks.py:37: DependencyWarning: SOCKS support in urllib3 requires the installation of optional dependencies: specifically, PySocks. For more information, see https://urllib3.readthedocs.io/en/latest/contrib.html#socks-proxies DependencyWarning telebottest.py:25: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(LedG, GPIO.OUT) telebottest.py:26: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(LedO, GPIO.OUT) Traceback (most recent call last): File "telebottest.py", line 66, in <module> bot.polling() File "/usr/local/lib/python2.7/dist-packages/telebot/__init__.py", line 263, in polling self.__threaded_polling(none_stop, interval, timeout) File "/usr/local/lib/python2.7/dist-packages/telebot/__init__.py", line 287, in __threaded_polling self.worker_pool.raise_exceptions() File "/usr/local/lib/python2.7/dist-packages/telebot/util.py", line 103, in raise_exceptions six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2]) File "/usr/local/lib/python2.7/dist-packages/telebot/util.py", line 54, in run task(*args, **kwargs) TypeError: 'str' object is not callable Exception in thread WorkerThread1 (most likely raised during interpreter shutdown):Exception in thread WorkerThread2 (most likely raised during interpreter shutdown):Exception in thread PollingThread (most likely raised during interpreter shutdown):root@raspberrypi:~/TelegramBot/telebottest# python telebottest.py /usr/local/lib/python2.7/dist-packages/urllib3/contrib/socks.py:37: DependencyWarning: SOCKS support in urllib3 requires the installation of optional dependencies: specifically, PySocks. For more information, see https://urllib3.readthedocs.io/en/latest/contrib.html#socks-proxies DependencyWarning telebottest.py:25: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(LedG, GPIO.OUT) telebottest.py:26: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(LedO, GPIO.OUT) Traceback (most recent call last): File "telebottest.py", line 66, in <module> bot.polling() File "/usr/local/lib/python2.7/dist-packages/telebot/__init__.py", line 263, in polling self.__threaded_polling(none_stop, interval, timeout) File "/usr/local/lib/python2.7/dist-packages/telebot/__init__.py", line 287, in __threaded_polling self.worker_pool.raise_exceptions() File "/usr/local/lib/python2.7/dist-packages/telebot/util.py", line 103, in raise_exceptions six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2]) File "/usr/local/lib/python2.7/dist-packages/telebot/util.py", line 54, in run task(*args, **kwargs) TypeError: 'str' object is not callable Exception in thread WorkerThread1 (most likely raised during interpreter shutdown):Exception in thread WorkerThread2 (most likely raised during interpreter shutdown): Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_innerTraceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner File "/usr/local/lib/python2.7/dist-packages/telebot/util.py", line 57, in run <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'Empty' File "/usr/local/lib/python2.7/dist-packages/telebot/util.py", line 61, in run <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'exc_info' 

  • Where is the exception and traceback exceptions? - Andrio Skur
  • @AndrioSkur, I'm sorry, I don’t really understand what you are talking about. I saw something about traceback when the script crashes, but I could not find the necessary information about this. - Fiveteen
  • This is what you need, add to the question the errors that are displayed when the script "crashes" - Igor Lavrynenko
  • The script does not fall. If the script crashes, it writes something like "Segmentation fault: .." in the console. The script to fall with the exception. You actually need something like this (it appears in the console before the end of the work) "Traceback (most recent call last): File" ", line 1, in int ('qwerty') ValueError: invalid literal for int () with base 10 : 'qwerty' ". - Andrio Skur
  • @AndrioSkur, Updated the question. PS Updated code. Replaced bot.message_loop (handle) with bot.polling (). So at least it starts. But when you press any of the buttons in the bot, it ends its work with errors. - Fiveteen

1 answer 1

I'm not an expert, but it seems you need Python version 3 to write a bot

  • one
    It is not necessary to write in the answers "it seems." Answers should be clear and unambiguous without any guesswork - andreymal