Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to make a telegram bot respond to the specific word in a sentence?
So, I'm trying to make my bot respond whenever a certain word is said in a sentence (the language is Serbian), so I made this code:

if (message.text.lower().find('lav') != -1):
        bot.send_message(,  "Najgore pivo...")
Now, the bot is doing what I would like, when some somebody says:

"Popio sam lav"

it responds as it should.
But, the problem is that the word "lav" is also part in other words, for example:

"Nebo je plavo"

so the bot responds here as well and I want to block that.

I am just starting to play with python, so I would really appreciate help, tnx :)
It's looking for any sequence of "lav" in the string. Here are two solutions.

1. Split the sentence up by white space. This will change the sentence to a list which you can iterate over, checking for the word.

sentence = message.text.lower()
if "lav" in sentence.split():
        bot.send_message(,  "Najgore pivo...")
2. Use a regular expression instead. "\blav\b" will do the trick.

import re

match_lav = re.compile("\blav\b")
matches = match_lav.findall(message.text.lower())

if len(matches) > 0:
        bot.send_message(,  "Najgore pivo...")

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to match partial sentence in long sentence Mekala 1 149 Jul-22-2020, 02:21 PM
Last Post: perfringo
  Telegram bot - Problem with database NoNameoN 1 99 Jul-13-2020, 06:39 AM
Last Post: Gribouillis
  Multiplayer call to Telegram bot AlexSn2020 2 181 Jul-01-2020, 10:35 AM
Last Post: AlexSn2020
  Bot coding telegram.ext Kumarkv 1 182 May-29-2020, 01:00 AM
Last Post: Kumarkv
  Python Speech recognition, word by word AceScottie 6 8,232 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Remove a sentence if it contains a word. lokhtar 6 461 Feb-11-2020, 04:43 PM
Last Post: stullis
  if-loop does not respond to input Kmarstein 1 289 Jan-28-2020, 09:29 PM
Last Post: Larz60+
  Delete specific lines contain specific words mannyi 2 410 Nov-04-2019, 04:50 PM
Last Post: mannyi
  print a word after specific word search evilcode1 8 573 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  how to get all the possible permutation and combination of a sentence in python sodmzs 1 757 Jun-13-2019, 07:02 AM
Last Post: perfringo

Forum Jump:

Users browsing this thread: 1 Guest(s)