Tuesday, 15 January 2013

Messages


Messages

Let’s introduce a second PyS60 module: messaging. It handles the
sending of SMS (Short Message Service) and MMS (Multimedia Messaging
Service) messages. Here we use only one function from the module:
messaging.sms send().
import messaging
messaging.sms_send("+14874323981", u"Greetings from PyS60")

To be able to send an SMS we need to import the module messaging
at the beginning of our script. We only need to pass two parameters to
the function sms send (): the telephone number of the recipient as a
string and the body of the message as a Unicode string.
In this concluding example (Example 8), we combine the UI elements
learned in this chapter and the function for sending SMS messages into a
useful little program.
Imagine you are at home, your fridge is empty and your friend is on
the way home passing a grocery store. He does not know which items to
buy. You want to send him a list of items by SMS while standing in front
of the fridge checking what is needed. Typing all the stuff into the phone
is just too much hassle and takes too much time.
This is where your PyS60 program, the Shopping List Assistant, comes
in. From a pre-defined list of items on your screen, you can select the
items needed using a selection list() dialog. A text input field lets
you type an additional greeting. After this, your shopping list is on its way
to your friend by SMS.

Example 8: Shopping list assistant
import appuifw, messaging
foods = [u"cheese", u"sausage", u"milk", u"banana", u"bread"]
choices = appuifw.multi_selection_list(foods,'checkbox',1)
shoppinglist = ""
for x in choices:
shoppinglist += foods[x] + " "
greetings = appuifw.query(u"Add some greetings?", "text", u"thanks!")
if greetings:
shoppinglist += greetings
print "Sending SMS: " + shoppinglist
messaging.sms_send("+1234567", shoppinglist)
appuifw.note(u"Shopping list sent")


First we import the modules appuifw and messaging. Next we
create the list foods, which includes every possible food item that can
be missing from the fridge. This list can be extended by you to as many
elements as you want. Remember that the elements need to be Unicode
strings, that is, they must be prefixed with the ‘u’ character.
In the next line, we generate our multi selection list() and
pass foods to it. We also enable the find pane. This displays the selection
list on the phone screen with all the goods, allowing us to select all the
items that need to be bought. The result of our selection is stored in the
variable choices. It is a tuple that holds the indices of the selected
items.
A for loop reads the selected items from the foods list, based on
the indices of the selected items in choices. We start with an empty
list, shoppinglist, to which we add the required items during each

iteration of the for loop. The += operator is a shorthand for the following
expression:
shoppinglist = shoppinglist + foods[x] + " "
In the line following the for loop, we create a query dialog of type
‘text’, meaning that the phone screen will show a text input field saying
‘Add some greetings?’ with the initial word ‘thanks!’. If the user chooses
to add a greeting, the string is added to the shopping list.
Finally, we want to send the shopping list by SMS. For this we use
the messaging.sms send() function to which we hand the mobile
phone number of the recipient and the list shoppinglist.
Once the phone has sent the SMS, we want a confirmation that the
SMS with our shopping list has been sent. We do this by generating a
note dialog of type ‘info’.
And that is all. Let’s hope our fridge will never be totally empty again.


Summary

In this chapter, we have introduced a set of native UI dialogs that
PyS60 offers. They got you involved with working examples of PyS60
programming with relatively little code to write. The final exercise showed
how to combine several elements into a simple but useful application,
the shopping list assistant.
In Chapter 4, we introduce principles of application building and move
into more fun applications. In many examples throughout the book, you
will find these UI elements again and see how they can be part of a fully
working application. At the same time, you learn how you can integrate
them into applications of your own.
Maybe you already have an idea of a simple application in which
some of these UI elements can be used. Why not try it out right away?















No comments:

Post a Comment