I am new to Python and coding in general. I'm trying to code up simple Telegram bot on Python 3.10.6 using pyTelegramBotAPI 4.7.0.
This is my code:
import telebot;
bot = telebot.TeleBot('*there was my Telegram bot token*');
name = '';
surname = '';
age = 0;
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id,"Hi! I'm another bot for practicing skill :)")
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
if message.text == "hi":
bot.send_message(message.from_user.id, "How can I help you?")
elif message.text == "/help":
bot.send_message(message.from_user.id, "Registration - /reg")
elif message.text == '/reg':
bot.send_message(message.from_user.id, "Nice! Lets sign you up! What is your name?");
bot.register_next_step_handler(message, get_name);
else:
bot.send_message(message.from_user.id, "Sorry, I don't understand that. Write /help")
def get_name(message):
global name;
name = message.text;
bot.send_message(message.from_user.id, 'Okay! What is your surname?');
bot.register_next_step_handler(message, get_surname);
def get_surname(message):
global surname;
surname = message.text;
bot.send_message('What is your age?');
bot.register_next_step_handler(message, get_age);
def get_age(message):
global age;
while age == 0:
try:
age = int(message.text)
except Exception:
bot.send_message(message.from_user.id, 'Oops! I need only number.');
bot.send_message(message.from_user.id, 'You are ' str(age) 'yo, your full name is ' name ' ' surname '?')
bot.delete_webhook()
bot.infinity_polling()
It's good working just right before getting surname step: Screenshot_part1 Screenshot_part2
So, right after I sent text as surname it's just stopped registration process. Bot still working, sending any other text will return
Sorry, I don't understand that. Write /help
In my IDLE Shell I can see error:
2022-08-16 18:33:33,773 (__init__.py:878 MainThread) ERROR - TeleBot:
"Infinity polling exception: TeleBot.send_message() missing 1 required
positional argument: 'text'" 2022-08-16 18:33:33,895 (__init__.py:880
MainThread) ERROR - TeleBot: "Exception traceback: Traceback (most
recent call last): File
"C:\Users\Кирилл\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py",
line 874, in infinity_polling
self.polling(non_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout, File
"C:\Users\Кирилл\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py",
line 946, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout, File
"C:\Users\Кирилл\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py",
line 1021, in __threaded_polling
raise e File "C:\Users\Кирилл\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py",
line 977, in __threaded_polling
self.worker_pool.raise_exceptions() File "C:\Users\Кирилл\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\util.py",
line 154, in raise_exceptions
raise self.exception_info File "C:\Users\Кирилл\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\util.py",
line 98, in run
task(*args, **kwargs) File "C:/Users/Кирилл/Desktop/bot/bot.py", line 32, in get_surname
bot.send_message('What is your age?'); TypeError: TeleBot.send_message() missing 1 required positional argument: 'text'
"
CodePudding user response:
(re-posting comment as answer)
You have bot.send_message(message.from_user.id, 'some text')
all over the place; but the line in the error, bot.send_message('What is your age?');
has no message.from_user.id argument
. I assume you forgot to pass in a user ID. You are passing in text as the user ID, and are not passing in anything to the text argument at all.