R
R
RaymanPy2019-01-13 18:41:26
Python
RaymanPy, 2019-01-13 18:41:26

How to make dialogue chains in Telegram Bot?

I'm using pyTelegramBot and I don't need to make chained dialogs.
I found one way, but when using it, an error occurs:

2019-01-13 19:29:22,752 (util.py:64 WorkerThread2) ERROR - TeleBot: "TypeError occurred, args=("'NoneType' object is not callable",)
Traceback (most recent call last):
  File "C:\Users\Rizvan\Desktop\Telegram Bot\venvbot\lib\site-packages\telebot\util.py", line 58, in run
    task(*args, **kwargs)
TypeError: 'NoneType' object is not callable
"
Traceback (most recent call last):
  File "C:\Users\Rizvan\Desktop\Telegram Bot\bot.py", line 93, in <module>
    bot.polling(none_stop=True)
  File "C:\Users\Rizvan\Desktop\Telegram Bot\venvbot\lib\site-packages\telebot\__init__.py", line 264, in polling
    self.__threaded_polling(none_stop, interval, timeout)
  File "C:\Users\Rizvan\Desktop\Telegram Bot\venvbot\lib\site-packages\telebot\__init__.py", line 288, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "C:\Users\Rizvan\Desktop\Telegram Bot\venvbot\lib\site-packages\telebot\util.py", line 107, in raise_exceptions
    six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
  File "C:\Users\Rizvan\Desktop\Telegram Bot\venvbot\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\Rizvan\Desktop\Telegram Bot\venvbot\lib\site-packages\telebot\util.py", line 58, in run
    task(*args, **kwargs)
TypeError: 'NoneType' object is not callable
[Finished in 11.6s]

The same error occurs when using conditions.
Which I use for validation.
Here is my (shit-) code
. The error is pointing to the line bot.polling(none_stop=True)
bot.py
import telebot
from telebot import types
import json
from models import *

token = 'мой токен'
bot = telebot.TeleBot(token)


@bot.message_handler(commands=['start'])
def start(message):
   
  markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
  markup.row('Сформировать заказ')
  
  
  msg = bot.send_message(message.chat.id,'Выберите вариант',reply_markup=markup)
  bot.register_next_step_handler(msg, user_enter)

@bot.message_handler(regexp='сформировать заказ')
def user_enter(message):
  if message.text.lower() == 'сформировать заказ':
    catalog = session.query(Catalog).all()
    markup = types.ReplyKeyboardMarkup()
    for product in catalog:
      markup.row(product.name)
    msg = bot.send_message(message.chat.id,'Выберите товар', reply_markup=markup)
    bot.register_next_step_handler(msg, user_choice)



def user_choice(message):
  catalog = session.query(Catalog).all()
  markup = types.ReplyKeyboardMarkup()

  for product in catalog:
    if product.name == message.text :
  
      msg = bot.send_message(message.chat.id, 'Введите количество')
      
      bot.register_next_step_handler(msg,product_count(message=message,product=product))


def product_count(message, product):
  
  count = message.text

  msg = bot.send_message(message.chat.id, 'Товар добавлен в корзину , вы можете продолжить покупки или сделать заказ')
  bot.register_next_step_handler(msg, add_to_basket(message,product,count))



def add_to_basket(message,product,count):
  prod = Basket(message.from_user.id)
  prod.prods.append(product)
  session.add(prod)
  session.commit()
  user_enter(message)

if __name__ == '__main__':
  bot.polling(none_stop=True)

After entering the quantity of the product, the same error occurs

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Trainin, 2019-01-15
@RaymanPy

I’ll immediately send a link to my template for my bots, there is an answer, but I’ll explain it even lower Github
. - you call this connection when calling the bot.polling() function), you need to import the apihelper module and make a connection through a proxy server (I used the method with SOCKS proxies, they can be found for free in the cart itself, there are bots that give out, google .) and then everything will work. Now the bot simply cannot connect to the telegram server, which is why an error pops up.
The code for apihelper itself:

spoiler
from telebot import apihelper

apihelper.proxy = {'http':'http://10.10.1.10:3128'}
#или вариант с socks5
apihelper.proxy = {'https':'socks5://userproxy:[email protected]_address:port'}

And I recommend moving keyboards and repetitive code blocks into separate files and importing them where necessary, this will make the code itself more readable and cleaner, and development more convenient.
In general, look at my github, there seems to be a good example of a bot, it will save you a little time.
Good luck in development)
Py.Sy. - Off. github library there was an answer to your question in the Proxy section. Read the documentation before development, it's useful.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question