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

Asked

Viewed 355 times

0

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

Example:

  • User: I think the Joker knows more about this
  • Bot: What subject?
  • User: About the seal killing in the backlands of Ceará, 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."
  • You need to have a list of predefined commands. If you’re looking to do something like the Google Assistant (which answers countless questions formulated in the most different ways as well as being error-tolerant), have a million dollar budget first.

  • I already own a. yml file with numerous predefined questions and answers. It doesn’t need to be error-tolerant. It’s not something so "astronomical" Google Assistant style. Chatbot itself works smoothly, answering questions or giving a standard answer when you don’t have an answer in your database. But, the intention is for him to respond in the group, only when his name was mentioned in a sentence and not, to answer any message that is sent in the group, as is what happens.

1 answer

2

What I notice in your code is that the first condition does not check if there is "Joker" in the message. I assume he’s always tinkering "Hm, you’re talking about ..."

To correct this only, 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 the message
  2. If the message contains more than 50 characters
  3. (I presume) if you exceeded your quota of IBM Watson

The way it is, Joker needs to have the first capital letter to fire the code. I suppose the ideal would be for it to 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 deleted the character requirement, because 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 to say:

User: Joker
Bot: Something is not right [...]

Edit 3: changes the order of the conditions, because the second condition nullified the third.

  • As the characters were insufficient in the comment, I put as a response below.

  • So, @Seumadruga, I’m using Python 3.7.4. I’ve updated the code. Bot persists in reacting to any message sent to the group. That sounds like Chaves. It had to be Chaves.

  • There’s been some progress. The Bot reacts as an error to any message that does not contain Joker in the sentence and returns one of the predefined answers to questions he has not yet learned. User: Hello Bot: Something is wrong Type /info to learn more. User: Hello Joker Bot: I didn’t understand, you can tell me how I should have responded?

  • Traceback (Most recent call last): File "bot/application.py", line 19, in <module> from bot.Communication import Communication File "E: 01 - Chatbot project Master bot Communication.py", line 67 joker_na_message = True Indentationerror: expected an indented block

  • @Jacob error of identation. Try now.

  • Now the following happens: File "E: 01 - Chatbot Master bot project Communication.py", line 74 Return f"Hmm, you’re talking about {top_answer}" Syntaxerror: 'Return' Outside Function

  • @Jacob Apparently, there is nothing wrong. Have you messed with the identation? I’ve corrected it. Copy it again and see if it works.

  • No more errors. However, Bot responds with "None" for messages that do not contain "Joker". And the ones that contain Joker in the sentence, he answers with one of the predefined answers, that he hasn’t learned yet. I don’t know if it would be possible, but he would answer with the answers already predefined. Like, he would understand the question, ignoring the Joker in the sentence. The Joker in the sentence, makes him not know how to give an answer among all possible registered. Sorry if it seems confusing.

  • @Jacob is possible, but he is outside the scope of this question. Ask another question and elaborate this question, and let me know here.

  • So @Seumadruga. I posted the new question.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.