Wednesday, 16 January 2013

Forwarding Messages


Forwarding Messages

As you know from everyday usage of your mobile phone, an arriving text
message triggers a sound notification, as well as a popup note saying that
a new message has arrived.
However, if you make a program that handles messages automatically,
the notification may not be desirable. Luckily, the sound and the dialog
can be avoided if the message is deleted immediately in the callback
function when the new message has arrived.
70 APPLICATION BUILDING AND SMS INBOX
In Example 18, we demonstrate this feature with a filtering SMS gateway.
The gateway receives a new message, removes all nasty words in it
and forwards the censored message to another phone.
Example 18: Filtering SMS gateway
import messaging, inbox, e32
PHONE_NUMBER = "+18982111111"
nasty_words = ["Java", "C++", "Perl"]
def message_received(msg_id):
box = inbox.Inbox()
msg = box.content(msg_id)
sender = box.address(msg_id)
box.delete(msg_id)
for word in nasty_words:
msg = msg.replace(word, "XXX")
messaging.sms_send(PHONE_NUMBER, msg)
print "Message from %s forwarded to %s" %\
(sender, PHONE_NUMBER)
box = inbox.Inbox()
box.bind(message_received)
print "Gateway activated"
app_lock = e32.Ao_lock()
app_lock.wait()
This script should disable any notifications caused by arriving SMS messages.
As in the previous example, the function message received()
is called when a new message arrives.
The function message received() fetches the contents of the new
message and deletes it immediately from the inbox. Nasty words are
replaced with ‘XXX’ and the cleaned message is forwarded to another
phone, specified in the PHONE NUMBER. A log message is printed out for
each forwarded SMS, which shows both the sender’s and the recipient’s
phone numbers.
Note that you should not specify your own number in the PHONE
NUMBER. This would make the program forward messages to itself,
resulting in an infinite loop!

No comments:

Post a Comment