What I’m trying to do:
I want to add the details of an incoming text message to my data table only if it is a new message.
The way I will determine if it’s a new message is by the message_sid, which is unique to each message.
What I’ve tried and what’s not working:
The get_incoming_messages() function gets called every two seconds. It’s successfully putting the text message details into my table, but it’s replicating it every two seconds. I only want new messages to append to the table. So basically: if unique SID, add to the table, else, pass.
Code Sample:
@anvil.server.callable
def get_incoming_messages():
x = client.messages.stream(to=1234567890, limit=1)
return [app_tables.inbound_text_messages.add_row(phone_number=1234567890, message=str(x.body), \
date_received=(x.date_sent), message_sid=(x.sid)) for x in x if not x.sid == app_tables.inbound_text_messages.search(
message=q.full_text_match(x.sid))]
If I understand currently, you want a program that will add a new row only if the message is not there, right? Because there is a very simple method to do his
if app_tables.inbound_text_messages.get(message=q.full_text_match(<your message string>)==None:
...
In addition to what @divyeshlakhotia corrected, you are also using the variable name x twice, for both the set of messages and an individual message. You also want to be checking whether the sid matches any existing message_sids, not messages. Try this:
def add_message(x):
return app_tables.inbound_text_messages.add_row(
phone_number=1234567890,
message=str(x.body),
date_received=(x.date_sent),
message_sid=(x.sid)
)
def has_new_sid(x):
return app_tables.inbound_text_messages.get(message_sid=q.full_text_match(x.sid)) == None
@anvil.server.callable
def get_incoming_messages():
xs = client.messages.stream(to=1234567890, limit=1)
return [add_message(x) for x in xs if has_new_sid(x)]