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?
#1
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(message.chat.id,  "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 :)
Quote
#2
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(message.chat.id,  "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(message.chat.id,  "Najgore pivo...")
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Delete specific lines contain specific words mannyi 2 135 Nov-04-2019, 04:50 PM
Last Post: mannyi
  print a word after specific word search evilcode1 8 180 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 299 Jun-13-2019, 07:02 AM
Last Post: perfringo
  Sentence maker help bidoofis 2 427 Feb-08-2019, 03:59 AM
Last Post: bidoofis
  wont print last sentence.. mitmit293 2 326 Jan-27-2019, 05:38 PM
Last Post: aakashjha001
  Generating all simple sentence possibilities Max_77 3 688 Oct-10-2018, 11:35 AM
Last Post: Max_77
  PigLatin Sentence Translator Help 2skywalkers 7 1,507 Jun-23-2018, 12:46 AM
Last Post: 2skywalkers
  Question about telegram bot Romank 0 567 Mar-29-2018, 09:36 PM
Last Post: Romank
  difference between word: and word[:] in for loop zowhair 2 893 Mar-03-2018, 07:24 AM
Last Post: zowhair
  Telegram code help AaronJonk 0 1,281 Oct-05-2017, 02:44 PM
Last Post: AaronJonk

Forum Jump:


Users browsing this thread: 1 Guest(s)