R
R
reddfan2020-02-17 17:27:24
Python
reddfan, 2020-02-17 17:27:24

Telegram bot, response to the user if his message contains a word?

I'm trying to make a telegram bot, I need the bot to send a sticker to the user if his message contains a word, and does not match completely, how to implement this?
I am using pyTelegramBotAPI.
Example, the user sends a message, and if at least in some place of the message there is the word "Dima", a sticker is sent to the chat. Currently sending a sticker if the user's message consists only of "dima". Please help me figure it out. I can't find the answer for the life of me.

import telebot
import config
from telebot import types

bot = telebot.TeleBot(config.TOKEN)

@bot.message_handler(commands=['start'])
def welcome(message):
bot.send_message(message.chat.id, 'Hello' )

@bot.message_handler(content_types=['text'])
def lalala(message):
if message.text.lower() == 'dima' or message.text.lower() == 'dmitry' or message.text. lower() == 'dimon':
sti = open('stickers/sticker.webp', 'rb')
bot.send_sticker(message.chat.id, sti)

bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Web Dentist, 2020-02-17
@reddfan

names = ['дима', 'дмитрий', 'димоооооон']
for name in names:
    if name in message.text.lower(): 
        # ваш код

A
Andrey, 2020-02-17
@anerev

The code tag must be used (and know the basics of python)

@bot.message_handler(func=lambda message: 'дима' in message.text.lower(), content_types=['text'])
def lalala(message):
    sti = open('stickers/sticker.webp', 'rb')
    bot.send_sticker(message.chat.id, sti)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question