ChatBot respond only when a specific word is contained in the sentence

I am creating a chatbot on Telegram that can answer questions from users in the group. The bot needs a keyword to start chatting. That word would be his name, " Joker." Thus, only when the word Joker was mentioned in a sentence would he begin to interact with the users of the group or when a response was forwarded to him. Using Python 3.7.4.

Example:

  • user: I think the Joker understands more of this subject
  • Bot: what subject?
  • user: about the killing of seals in the Ceará hinterland, Joker.
  • Bot: What are you talking about?

But I have no idea how to implement this action.

def respond(self, message):
    """
   Receive message from user and returns corresponding answer.
   """
    if len(message) > 50 and self.watson_usage:
        top_answer = get_analysis(message)
        return f"Hmm, você está falando sobre {top_answer}"
    elif re.search("Joker", message) or len(message) > 0:
        return self.comm.get_response(self.clean(message))
    else:
        return "Algo de errado não está certo"\
               " Digite /info para saber mais."
Author: Jacob, 2019-12-20

1 answers

What I notice in your code, is that the first condition does not check if there is "joker" in the message. I assume it's always resonating " Hm, you're talking about ..."

To fix only this, change the first condition to:

if 'Joker' in message and len(message) > 50 and self.watson_usage:

If you don't understand Python, the condition checks:

  1. if 'Joker' is in message
  2. if the message contains more than 50 characters
  3. (I assume) if you exceeded your IBM quota Watson

The way it is, joker needs to have the first capital letter to fire the code. I suppose ideally it would respond to any combination of lowercase and uppercase.

In this case, this is the Code:

def respond(self, message):
    """
    Receive message from user and returns corresponding answer.
    """
    if re.search("joker", message, re.IGNORECASE):
        joker_na_mensagem = True
    else:
        joker_na_mensagem = False

    if joker_na_mensagem and len(message) > 50 and self.watson_usage:
        top_answer = get_analysis(message)
        return f"Hmm, você está falando sobre {top_answer}"
    elif joker_na_mensagem and len(message.strip()) == len('joker'):
        return "Algo de errado não está certo. Digite /info para saber mais."
    elif joker_na_mensagem:
        return self.comm.get_response(self.clean(message))

Edit 1: I changed the second condition, where (I assume) the bot would respond to any message that had more than 0 characters regardless of being mentioned. I eliminated the character requirement, since if 'Joker' is in the message, it means that it is greater than 0 characters.

Edit 2: I changed the last condition. Now, the function only returns the error message if someone mentions the bot and says nothing else. That is:

User: Joker
Bot: something wrong is not right [...]

Edit 3: changes the order of the conditions, as the second condition overrides the third.

 2
Author: Seu Madruga, 2019-12-21 21:04:02