我正在为 Telegram 频道制作一个机器人,它给了我错误:
类型错误:“str”对象不可调用
这是代码:
import telebot
from telebot import types
import random
bot = telebot.TeleBot('токен')
bal = 2
farm = random.randint(1, 5)
chisel = random.randint(1, 10)
# ans = ''.format(''.join(answers))
@bot.message_handler(commands=['start', 'restart'])
def start(m):
bot.send_message(m.chat.id, f'Привет {m.from_user.first_name}, меня зовут Лёша.\nНапиши /gl чтобы начать')
@bot.message_handler(commands=['gl'])
def gl(m):
mk = types.ReplyKeyboardMarkup(resize_keyboard=True)
btn1 = types.KeyboardButton('Помощь')
mk.add(btn1)
bot.send_message(m.chat.id, 'Выбери одну из игр\n'
'Угадай число /gtc\n'
'Угадай слово /gtw\n'
'Угадай игру /gtg\n'
'Крестики-нолики /xo', reply_markup=mk)
if m.text == 'Помощь':
bot.send_message(m.chat.id, 'Напишите /help')
@bot.message_handler(commands=['gtc'])
def gtc(m):
mk = types.ReplyKeyboardMarkup(resize_keyboard=True)
btn1 = types.KeyboardButton('↩Назад')
btn2 = types.KeyboardButton('Готово')
mk.add(btn1, btn2)
answers = []
answer = m.text
answers.append(answer)
mss = bot.send_message(m.chat.id, 'Напиши ставку: (Мин. ставка 2)', reply_markup=mk)
bot.register_next_step_handler(mss, answer)
if m.text == 'Готово':
mk = types.ReplyKeyboardMarkup(resize_keyboard=True)
btn1 = types.KeyboardButton('↩Назад')
btn2 = types.KeyboardButton('Готово')
mk.add(btn1, btn2)
answer1 = m.text
answers.append(answer1)
mss = bot.send_message(m.chat.id, 'Напиши любое число: (1-10)', reply_markup=mk)
bot.register_next_step_handler(mss, answer1)
if m.text == 'Готово':
mk = types.ReplyKeyboardMarkup(resize_keyboard=True)
btn1 = types.KeyboardButton('На главную')
mk.add(btn1)
if random == answer1:
bot.send_message(m.chat.id, f'🎉Вы выйграли{answer * 2}\nВаш баланс{answer * 2 + bal}', reply_markup=mk)
elif random < answer1 and random > answer1:
bot.send_message(m.chat.id, f'Увы вы проигарли{answer}\nВаш баланс{answer - bal}')
elif m.text == 'На главную':
mk = types.ReplyKeyboardMarkup(resize_keyboard=True)
btn1 = types.KeyboardButton('Помощь')
mk.add(btn1)
bot.send_message(m.chat.id, 'Выбери одну из игр\n'
'Угадай число /gtc\n'
'Угадай слово /gtw\n'
'Угадай игру /gtg\n'
'Крестики-нолики /xo', reply_markup=mk)
if m.text == 'Помощь':
bot.send_message(m.chat.id, 'Напишите /help')
elif m.text == '↩Назад':
mk = types.ReplyKeyboardMarkup(resize_keyboard=True)
btn1 = types.KeyboardButton('Помощь')
mk.add(btn1)
bot.send_message(m.chat.id, 'Выбери одну из игр\n'
'Угадай число /gtc\n'
'Угадай слово /gtw\n'
'Угадай игру /gtg\n'
'Крестики-нолики /xo', reply_markup=mk)
if m.text == 'Помощь':
bot.send_message(m.chat.id, 'Напишите /help')
else:
bot.send_message(m.chat.id, "Увы я не знаю такой команды")
@bot.message_handler(commands=['help'])
def help(m):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
btn1 = types.KeyboardButton('🎲Кубик')
btn2 = types.KeyboardButton('🎰Угадай число')
markup.add(btn1, btn2)
bot.send_message(m.chat.id, 'Выбери игру про которую хотите прочитать правила', reply_markup=markup)
@bot.message_handler(commands=['farm'])
def farm(m):
bot.send_message(m.chat.id, f'Вы получили {farm} коин(-а, -ов)\nВаш баланс {farm + bal}')
bot.infinity_polling()
完整错误:
2025-01-08 19:34:57,609 (__init__.py:1115 MainThread) ERROR - TeleBot: "Infinity polling exception: 'str' object is not callable"
2025-01-08 19:34:57,616 (__init__.py:1117 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\__init__.py", line 1110, in infinity_polling
self.polling(non_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\__init__.py", line 1198, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\__init__.py", line 1273, in __threaded_polling
raise e
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\__init__.py", line 1235, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\util.py", line 150, in raise_exceptions
raise self.exception_info
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\telebot\util.py", line 93, in run
task(*args, **kwargs)
TypeError: 'str' object is not callable"






