Question:
I am very little familiar with python yet. Actually the question:
I am writing a Telegram bot. Purpose – The bot should ask what command I want to give it and execute it. So far, I've given him the 5 most essential commands.
Namely: There is a Raspberry Pi , from which I run the script itself, while I did it through the Web application, I control the LED diode connected to the raspberry. There are functions
- Turn on green light
- Turn off green light
- Turn on orange light
- Turn off orange light
- Led reset function.
Everything works through the web application. I Telepot
using the Telepot
library. The challenge is to get rid of the Web
and switch to bot control. I also wrote a code that works with a bot, i.e. I can process and execute these functions from the bot. I want to make sure that I do not enter all the commands by hand, but that there are convenient buttons.
Now I made it so that at startup, 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. I also learned how to make buttons using the Telebot
library. Tell me how you can make them friends? (Use at the same time)
I am attaching 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)
Вот что происходит с момента запуска скрипта и до его окончания:
/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'
Answer:
I'm not an expert, but it seems you need version 3 of python to write a bot