Home python processing chat messages. Pytelegrambotapi

processing chat messages. Pytelegrambotapi




I’m trying to create a message handler. Which will be responsible for the received message if it is listed.
For example.
I add such part of the code to this code.

@ bot.message_handler (content_types = ['text'])
Def Lalala (Message):
  If Message.Chat.Type == 'Private':
    If Message.Text == 'Hi':
      Bot.Send_Message (Message.chat.id, yes I am glad to see you)
 bot.send_message (message.chat.id, forgive friend I do not understand you)

guys can you help me, implement this?

I try to paste it starting from 70 lines of the deployer swears at 73 lines.
I do not understand what indents need to be observed?
Sometimes even when the code is edited, it is not making changes to the tool on the tab.
Tab and space search services indicate an error, I know that the tabs do not use only spaces, I delete the specified space, and not how it does not finish writes that there are errors in tabulation.
How to solve it?

import telbot
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 ("🎲 Random number")
  Item2 = Types.KeyboardButton ("😊 How are you?")
  Item3 = Types.KeyboardButton ("💰Pill")
  Item4 = Types.KeyboardButton ("23")
  Markup.Add (Item1, Item2, Item3, Item4)
  Bot.Send_Message (message.chat.id, "Welcome! {0.first_name}! \ Nya - & lt; b & gt; {1.first_name} & lt; / b & gt;, Bot created to be an experimental rabbit." 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 == '💰Pill💰':
      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 == '🎲 Random number':
      Bot.Send_Message (Message.chat.id, 'I do not drink')
    ELIF Message.Text == '23':
      Bot.Send_Message (Message.chat.id, 'I do not know what to answer 😢')
    Elif Message.Text == '😊 How are you?':
      Markup = Types.inlineKeyboardMarkup (ROW_WIDTH = 2)
      Item1 = Types.inlineKeyboardButton ("Good", Callback_Data = 'Good')
      Item2 = Types.inlineKeyboardButton ("not very", callback_data = 'bad')
      Markup.Add (Item1, Item2)
      bot.send_message (message.chat.id, 'is great, how?', Reply_MarkUp = Markup)
      Bot.Send_Message (Message.chat.id, 'I do not know what to answer 😢')
@ Bot.callback_Query_Handler (Func = Lambda Call: True)
Def Callback_inline (Call):
    If Call.Message:
      If call.data == 'Good':
        Bot.Send_Message (call.message.chat.id, 'That's finely 😊')
      Elif call.data == 'Bad':
        Bot.Send_Message (call.message.chat.id, 'happens 😢')
      # Remove Inline Buttons
      Bot.edit_Message_Text (Chat_id = call.message.chat.id, message_id = call.message.message_id, text = "😊 How are you?",
        Reply_MarkUp = None)
      # Show alert.
      Bot.answer_Callback_Query (callback_query_id = call.id, show_alert = false,
        text = "This is a test notification !! 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, 'I'm not')
# RUN.
Bot.Polling (None_Stop = True)

Answer 1, Authority 100%

Telegram in this question is superfluous.
You ask about how to check if there is a string list.
This is done like this:

lst = ['a', 'b', 'c']
IF 'C' IN A:
  Print ("List LST contains 'C'")
print ("lst does not contain 'c'")

And in your case:

data = ['hello', 'hi', 'hello', 'yo']
@ bot.message_handler (content_types = ['text'])
def lalala (message):
  if message.chat.type == 'private':
    if message.text.lower () in data:
      bot.send_message (message.chat.id, 'Yes, I'm glad to see you')
  bot.send_message (message.chat.id, 'Sorry friend, I don't understand you')

I also advise you to lowercase the entire text of the message (str.lower ()) so that words written with capital letters are checked normally.

Answer 2, authority 33%

Here is 100% working code, in comparison with other methods, in my code a certain function will be responsible for each functionality

import telebot
import logging
bot = telebot.TeleBot (API_TOKEN)
logger = telebot.logger
telebot.logger.setLevel (logging.DEBUG)
words_list = ['lenin', 'jugashvili', 'tester']
@ bot.message_handler (func = lambda message: message.text.lower () in words_list)
def send_welcome (message):
  bot.reply_to (message, "Word detected in list!")
bot.polling ()

Answer 3

Try the last function fix like this

@ bot.message_handler (func = lambda message: True)
  def message_handler (message):
    if 'hello' in message.text:
      bot.send_message (message.chat.id, 'Hello')
      bot.send_message (message.chat.id, 'I'm not z')
  # RUN
  bot.polling (none_stop = True)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions