Python 3: find item in list and answer based on that

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Python 3: find item in list and answer based on that



I am trying to make a python script that listens to Facebook chat using fbchat and searches for the word 'cf'. If that word is detected in chat, I want to send a pre-defined message Answer1. See below for my code and the error I get:


fbchat


'cf'


Answer1


from fbchat import log, Client
from fbchat.models import *

wordImLookingFor = ['cf']

Answer1 =['Hello! how can i help you']

# Subclass fbchat.Client and override required methods
class EchoBot(Client):
def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
self.markAsDelivered(thread_id, message_object.uid)
self.markAsRead(thread_id)

log.info(" from in ".format(message_object, thread_id, thread_type.name))

# If you're not the author, echo
if author_id != self.uid:
if word in message_object:
print("The word is in the list!")
self.send(Message(text=answer1), thread_id=thread_id, thread_type=ThreadType.USER)


else:
print("The word is not in the list!")



client = EchoBot('user', 'pass')


client.listen()



Exception in parsing ...
Traceback (most recent call last):
...
File "C:/Users/John/Downloads/fbd2.py", line 22, in onMessage
if word in message_object:
TypeError: argument of type 'Message' is not iterable




2 Answers
2



Here's the API for fbchat.models.Message



I believe you're looking for the text field


if word in message_object.text:
print("The word is in the list!")



EDIT:



Simple Solution



For your next error, the in keyword is expecting a single string and not a list of strings. Since you're looking for several keywords in the message, execute that case statement for each word in the list.


in


if author_id != self.uid:
for word in wordImLookingFor:
if word in message_object.text:
print("The word is in the list!")
self.send(Message(text=answer1), thread_id=thread_id, thread_type=ThreadType.USER)
else:
print("The word is not in the list!")



Scaled Solution



Since you're going to eventually be searching for a set of multiple keywords, and presumably each keyword should elicit a different Answer, you can save some complexity by creating a dictionary of keyword:answer.


keywords = 'cf': 'Hello! how can i help you', 'key2': 'Answer2'

class EchoBot(Client):
def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
self.markAsDelivered(thread_id, message_object.uid)
self.markAsRead(thread_id)

log.info(" from in ".format(message_object, thread_id, thread_type.name))

if author_id != self.uid:
for word in message_object.text.split():
try:
self.send(Message(text=keywords[word]), thread_id=thread_id, thread_type=ThreadType.USER)
print(word + "is in the list!")
except KeyError:
print(word + "is not in the list!")





Thank you : this is the new error i get after i added .text :TypeError: 'in <string>' requires string as left operand, not list
– Giani Curcubeu
Aug 7 at 16:53





@GianiCurcubeu Check the definition wordImLookingFor = ['ch'], you initialize it as a list of strings instead of a single string
– unmrshl
Aug 7 at 16:56



wordImLookingFor = ['ch']





yes because 'wordImLookingFor = ['cf']' is just the first word, it will be a list of strings down the line as i will expand the list that it must look for, multiple triggers.
– Giani Curcubeu
Aug 7 at 16:59





if you know a better way for me to make a variable containing multiple words to look for please enlighten me!
– Giani Curcubeu
Aug 7 at 17:02





It worked thank you very much!
– Giani Curcubeu
Aug 7 at 17:12



Your exception is at self.send(Message(text=answer1)



It looks like you are trying to send text as a LIST instead of a string.



Message cannot iterate over the list, but wants a simple string.


Answer1 =['Hello! how can i help you']
Answer1 = 'Hello! How can I help you?'





Thank you i edited that i get the same error : Traceback (most recent call last): File "C:Anaconda3libsite-packagesfbchatclient.py", line 1542, in _parseMessage thread_id=thread_id, thread_type=thread_type, ts=ts, metadata=metadata, msg=m) File "C:/Users/John/Downloads/fbd2.py", line 25, in onMessage if word in message_object.text: TypeError: 'in <string>' requires string as left operand, not list
– Giani Curcubeu
Aug 7 at 17:04






Possibly the difference in Answer1 vs answer1 Is there a reason Answer1 is not passed explicitly to the class?
– GeorgeLPerkins
Aug 7 at 17:18





If your answer is always 'Hello! How can I help you?' is there a reason you are defining it like this? If you are going to have different words to look for and possibly different answers, you should set up a dictionary. wordstolookfor = 'cf': 'Hello! How can I help you?', 'word2': 'response2'
– GeorgeLPerkins
Aug 7 at 17:22





I will ,thank you, the answers i want to give are based on the answer received in chat, i try to lead the conversation to simple yes and no answers and based on that call an reposnse or the other. but as i am a simple python noob that is just learning. i'm starting with 1 chat first response
– Giani Curcubeu
Aug 7 at 17:38







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS