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
  Delete specific lines contain specific words mannyi 2 179 Nov-04-2019, 04:50 PM
Last Post: mannyi
  print a word after specific word search evilcode1 8 236 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  Sentence maker help bidoofis 2 454 Feb-08-2019, 03:59 AM
Last Post: bidoofis
  wont print last sentence.. mitmit293 2 356 Jan-27-2019, 05:38 PM
Last Post: aakashjha001
  PigLatin Sentence Translator Help 2skywalkers 7 1,569 Jun-23-2018, 12:46 AM
Last Post: 2skywalkers
  Question about telegram bot Romank 0 595 Mar-29-2018, 09:36 PM
Last Post: Romank
  difference between word: and word[:] in for loop zowhair 2 938 Mar-03-2018, 07:24 AM
Last Post: zowhair
  Telegram code help AaronJonk 0 1,321 Oct-05-2017, 02:44 PM
Last Post: AaronJonk
  Python Speech recognition, word by word AceScottie 5 6,632 Aug-21-2017, 12:37 PM
Last Post: DeaD_EyE
  How to make a script to find a certain word in text files in a whole directory ? RandoomDude 2 3,398 Apr-27-2017, 10:27 AM
Last Post: wavic

Forum Jump:

Users browsing this thread: 1 Guest(s)