@sateray

Не работает бот в Телеграмме?

Что произошло? Объясняю, писал я код (по гайду), вроде написал. Запускаю выдаёт пару синтаксических ошибок, я их исправляю и всё ошибок нет, а бот не отвечает. Кнопки сами есть, а после нажатия ничего. Просьба помочь и объяснить в чём ошибка (только просьба объяснять на понятном новичку языке), спасибо.
import telebot
from telebot import types
import random
bot = telebot.TeleBot('token')
@bot.message_handler(commands=['start'])
def start(message):
	markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
	item1 = types.KeyboardButton('рандом')
	item2 = types.KeyboardButton('курсы')
	item3 = types.KeyboardButton('инфа')
	item4 = types.KeyboardButton('другое')

	markup.add(item1,item2,item3,item4)

	bot.send_message(message.chat.id,'Пивет,{0.first_name}!'.format(message.from_user),reply_markup = markup)

@bot.message_handler(content_type=['text'])
def bot_message(meessage):
	if message.chat.type == 'private':
		if message.text == 'рандом':
			bot.send_message(message,chat.id,'ваше число'+str(random.randint(o,1000)))
	elif message.text == 'курсы':
		markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
		item1 = types.KeyboardButton('доллар')
		item2 = types.KeyboardButton('евро')
		item3 = types.KeyboardButton('назад')
		markup.add(item1,item2,item3)

		bot.send_message(message.chat.id,'курсы', reply_markup = markup)

	elif message.text == 'инфа':
		markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
		item1 = types.KeyboardButton('о боте')
		item2 = types.KeyboardButton('что в коробке')
		item3 = types.KeyboardButton('назад')
		markup.add(item1,item2,item3)

		bot.send_message(message.chat.id,'инфа', reply_markup = markup)

	elif message.text == 'другое':
		markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
		item1 = types.KeyboardButton('настройка')
		item2 = types.KeyboardButton('подписка')
		item3 = types.KeyboardButton('стикер')
		item4 = types.KeyboardButton('назад')
		markup.add(item1,item2,item3,item4)

		bot.send_message(message.chat.id,'инфа', reply_markup = markup)
		
	elif message.text== 'назад':
		markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
		item1 = types.KeyboardButton('рандом')
		item2 = types.KeyboardButton('курсы')
		item3 = types.KeyboardButton('инфа')
		item4 = types.KeyboardButton('другое')
		markup.add(item1,item2,item3,item4)
		bot.send_message(message.chat.id,'Пивет,{0.first_name}!'.format(message.from_user),reply_markup = markup)
bot.polling(none_stop=True)
  • Вопрос задан
  • 86 просмотров
Решения вопроса 1
@kpechenenko
Ошибки, допущенные при написании кода:
- У аргумента функций `bot_message` одно имя, внутри функции пытаетесь обратиться к другому.
- Запятая вместо точки при получении id чата.
- Некорректная числовая граница диапазона для получения случайного значения в `random.randint`.
- Имя параметра 'content_type'.

После исправления получите следующее:
import telebot
from telebot import types
import random


bot = telebot.TeleBot( 'your token here')


@bot.message_handler(commands=['start'])
def start(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1 = types.KeyboardButton('рандом')
    item2 = types.KeyboardButton('курсы')
    item3 = types.KeyboardButton('инфа')
    item4 = types.KeyboardButton('другое')
    markup.add(item1, item2, item3, item4)
    bot.send_message(message.chat.id, f'Привет, {message.from_user.first_name}!', reply_markup=markup)


@bot.message_handler(content_types=['text'])
def bot_message(message):
    if message.chat.type == 'private':
        if message.text == 'рандом':
            bot.send_message(message.chat.id, f'Ваше число {random.randint(0, 1000)}')

        elif message.text == 'курсы':
            markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
            item1 = types.KeyboardButton('доллар')
            item2 = types.KeyboardButton('евро')
            item3 = types.KeyboardButton('назад')
            markup.add(item1, item2, item3)
            bot.send_message(message.chat.id, 'курсы', reply_markup=markup)

        elif message.text == 'инфа':
            markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
            item1 = types.KeyboardButton('о боте')
            item2 = types.KeyboardButton('что в коробке')
            item3 = types.KeyboardButton('назад')
            markup.add(item1, item2, item3)
            bot.send_message(message.chat.id, 'инфа', reply_markup=markup)

        elif message.text == 'другое':
            markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
            item1 = types.KeyboardButton('настройка')
            item2 = types.KeyboardButton('подписка')
            item3 = types.KeyboardButton('стикер')
            item4 = types.KeyboardButton('назад')
            markup.add(item1, item2, item3, item4)
            bot.send_message(message.chat.id, 'инфа', reply_markup=markup)

        elif message.text == 'назад':
            markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
            item1 = types.KeyboardButton('рандом')
            item2 = types.KeyboardButton('курсы')
            item3 = types.KeyboardButton('инфа')
            item4 = types.KeyboardButton('другое')
            markup.add(item1, item2, item3, item4)
            bot.send_message(message.chat.id, f'Привет, {message.from_user.first_name}!', reply_markup=markup)


bot.polling(none_stop=True)
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы