eternnoir / pyTelegramBotAPI

Python Telegram bot api.
GNU General Public License v2.0
8.07k stars 2.03k forks source link

Please add boot.message_handler message. #783

Closed correligionist closed 4 years ago

correligionist commented 4 years ago

Please answer these questions before submitting your issue. Thanks!

  1. What version of pyTelegramBotAPI are you using? LAST

  2. python 3.8

  3. What version of python are you using? 3.8

Hello all. I am make bot for telegram to used pytelegrambotapi. I am need help for add handller request for issues messagge recieved to sender. https://pastebin.com/ia4nPKM4 SOURCE CODE I'm trying to create a message handler. Which will respond to the received message if it is in the list. For example. I add this part of the code to this code. @boot.message_handler(content_type=['text'])

def lalala(message): if message.chat.type == 'private': if message.text = = 'HELLO': bot.send_message(message.chat.id Yes, I'm glad to see you) else: bot.send_message(message.chat.id Sorry friend I don't understand you)

Can you guys help me implement this?

BakhodirIsmatov commented 4 years ago

@bot.message_handler(func=lambda message: True) def message_handler(message): if 'hello' in message.text: bot.send_message(message.chat.id, "Have a nice day!")`

correligionist commented 4 years ago

@bot.message_handler(func=lambda message: True) def message_handler(message): if 'hello' in message.text: bot.send_message(message.chat.id, "Have a nice day!")`

Hello, bro! I am used te code. And alert error to "TAB"

`import telebot
import config
import random

from telebot import types

bot = telebot.TeleBot(config.TOKEN)

@bot.message_handler(commands=['start'])
def welcome(message):
    sti = open('avatar1459.jpg', 'rb')
    bot.send_sticker(message.chat.id, sti)

    # keyboard
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1 = types.KeyboardButton("🎲 Рандомное число")
    item2 = types.KeyboardButton("😊 Как дела?")
    item3 = types.KeyboardButton("💰Пополнить💰")
    item4 = types.KeyboardButton("23")

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

    bot.send_message(message.chat.id, "Добро пожаловать, {0.first_name}!\nЯ - <b>{1.first_name}</b>, бот созданный чтобы быть подопытным кроликом.".format(message.from_user, bot.get_me()),
        parse_mode='html', reply_markup=markup)

@bot.message_handler(content_types=['text'])

def lalala(message):
    if message.chat.type == 'private':
        if message.text == '💰Пополнить💰':
            bot.send_message(message.chat.id, str(random.randint(0,100)))
            bot.send_message(message.chat.id, str(random.randint(0,100)))   
        elif message.text == '🎲 Рандомное число':
            bot.send_message(message.chat.id, 'Я не пью')
        elif message.text == '23':    
            bot.send_message(message.chat.id, 'Я не знаю что ответить 😢')  

        elif message.text == '😊 Как дела?':

            markup = types.InlineKeyboardMarkup(row_width=2)
            item1 = types.InlineKeyboardButton("Хорошо", callback_data='good')
            item2 = types.InlineKeyboardButton("Не очень", callback_data='bad')

            markup.add(item1, item2)

            bot.send_message(message.chat.id, 'Отлично, сам как?', reply_markup=markup)
        else:
            bot.send_message(message.chat.id, 'Я не знаю что ответить 😢')

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    try:
        if call.message:
            if call.data == 'good':
                bot.send_message(call.message.chat.id, 'Вот и отличненько 😊')
            elif call.data == 'bad':
                bot.send_message(call.message.chat.id, 'Бывает 😢')

            # remove inline buttons
            bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="😊 Как дела?",
                reply_markup=None)

            # show alert
            bot.answer_callback_query(callback_query_id=call.id, show_alert=False,
                text="ЭТО ТЕСТОВОЕ УВЕДОМЛЕНИЕ!!11")

    except Exception as e:
        print(repr(e))
@bot.message_handler(func=lambda message: True)
def message_handler(message):
    if 'hello' in message.text:
bot.send_message(message.chat.id, 'Я не з')

# RUN
bot.polling(none_stop=True)
`

What to edit code to run normal work?

correligionist commented 4 years ago
import telebot
import config
import random

from telebot import types

bot = telebot.TeleBot(config.TOKEN)

@bot.message_handler(commands=['start'])
def welcome(message):
    sti = open('avatar1459.jpg', 'rb')
    bot.send_sticker(message.chat.id, sti)

    # keyboard
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1 = types.KeyboardButton("🎲 Рандомное число")
    item2 = types.KeyboardButton("😊 Как дела?")
    item3 = types.KeyboardButton("💰Пополнить💰")
    item4 = types.KeyboardButton("23")

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

    bot.send_message(message.chat.id, "Добро пожаловать, {0.first_name}!\nЯ - <b>{1.first_name}</b>, бот созданный чтобы быть подопытным кроликом.".format(message.from_user, bot.get_me()),
        parse_mode='html', reply_markup=markup)
@bot.message_handler(func=lambda message: True)

def message_handler(message):
    if message.chat.type == 'private':
        if 'hello' in message.text:
            bot.send_message(message.chat.id, "Have a nice day!") 

@bot.message_handler(content_types=['text'])

def lalala(message):
    if message.chat.type == 'private':
        if message.text == '💰Пополнить💰':
            bot.send_message(message.chat.id, str(random.randint(0,100)))
            bot.send_message(message.chat.id, str(random.randint(0,100)))   
        elif message.text == '🎲 Рандомное число':
            bot.send_message(message.chat.id, 'Я не пью')
        elif message.text == '23':    
            bot.send_message(message.chat.id, 'Я не знаю что ответить 😢')  

        elif message.text == '😊 Как дела?':

            markup = types.InlineKeyboardMarkup(row_width=2)
            item1 = types.InlineKeyboardButton("Хорошо", callback_data='good')
            item2 = types.InlineKeyboardButton("Не очень", callback_data='bad')

            markup.add(item1, item2)

            bot.send_message(message.chat.id, 'Отлично, сам как?', reply_markup=markup)
        else:
            bot.send_message(message.chat.id, 'Я не знаю что ответить 😢')

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    try:
        if call.message:
            if call.data == 'good':
                bot.send_message(call.message.chat.id, 'Вот и отличненько 😊')
            elif call.data == 'bad':
                bot.send_message(call.message.chat.id, 'Бывает 😢')

            # remove inline buttons
            bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="😊 Как дела?",
                reply_markup=None)

            # show alert
            bot.answer_callback_query(callback_query_id=call.id, show_alert=False,
                text="ЭТО ТЕСТОВОЕ УВЕДОМЛЕНИЕ!!11")

    except Exception as e:
        print(repr(e))
# RUN
bot.polling(none_stop=True)

I am use code WORK! Not work Button!!! What run for work message and button?

BakhodirIsmatov commented 4 years ago

напиши с telegram @baxa94