sms2twitter gate in 5 minutes
The purpose of this article is to demonstrate the capabilities of Python for s60 for working with SMS messages and databases. To make it more interesting, let’s take a look at a real example, create an application that will read new smartphone messages, parse them, save phone numbers with logins and passwords in your database, and post tweets that came from these numbers.
What else can this come in handy for? Nowadays, SMS services are used quite widely. There are a lot of ways to implement them. I recently had a need to implement a service in which customers could send an order number by SMS and their status would come back. The problem was solved like this: for 500 rubles, an old Nokia 7610 was bought at a flea market in a crumbling state, half of the buttons did not work, there were problems with sound, etc. A SIM card was inserted from a local operator with an unlimited SMS package. Now this device will play the role of a server that will operate 24/7, with an uninterrupted power supply (battery), and will not know the problems with cooling :)
Everything is simple here. There is an inbox module that allows us to work with messages on our phone. This module has the Inbox class of the same name, which allows you to read the message, sender and time. Each message has its own ID. A list of IDs of all messages can be obtained by the sms_messages () function. But we are more interested in the bind function, by which we specify a function that will be called immediately as soon as we receive a new message.
It will look like this:
I would like to mention the features of the address in the message. If we received a message whose sender’s number is in the phone’s notebook, then it will return a name, not a number, to address. If there is no subscriber and notebook, then we will get a line with a number.
Through python, it is possible to work with the native symbian database, dbms, which is a relational database and supports sql syntax. I had to work 600-800 thousand records on the relatively old smartphone nokia 3230, which pretty well coped with this amount of data.
There are 2 modules for working with e32db databases, which works directly with the database and e32dbm, which is an add-on for e32db and implements a key-value database interface. Despite the fact that in this case we will use e32dbm, I would like to show how to work with e32db. Those who write in python are probably used to DB API 2, so the principles of working with this database will seem a bit strange.
E32db has 2 classes. One for writing to the database, the other for reading.
Creating a database, creating tables and filling them out looks like this:
Do not forget that the path to the database and queries must be in Unicode.
Reading from a database is a bit more complicated.
For the application to work, e32dbm is enough for us, working with which is as simple as possible:
Let's take as a basis our code, which receives SMS messages. Add the following lines to the beginning:
It remains to write a function that will parse messages. In order for the subscriber to be able to work with this gate, his number must be associated with an account on Twitter. Let's not go through all possible authorization options and argue about security, but simply enter two commands: add login password and delete. The first command sent to the phone will add the username and password associated with the phone number to the database, and the second will be deleted. Accordingly, after such authorization, the user can simply write to the SMS gate number, which will go to Twitter.
and add the line to the incoming_callback function:
And it remains to implement the addition of a tweet, as we did in this topic.
Now our gate is ready. Optionally, it can be packaged in sis and filed in terms of error handling.
We take the source code here , and the ready sis is here.
Fresh ideas to you!
What else can this come in handy for? Nowadays, SMS services are used quite widely. There are a lot of ways to implement them. I recently had a need to implement a service in which customers could send an order number by SMS and their status would come back. The problem was solved like this: for 500 rubles, an old Nokia 7610 was bought at a flea market in a crumbling state, half of the buttons did not work, there were problems with sound, etc. A SIM card was inserted from a local operator with an unlimited SMS package. Now this device will play the role of a server that will operate 24/7, with an uninterrupted power supply (battery), and will not know the problems with cooling :)
Work with SMS messages
Everything is simple here. There is an inbox module that allows us to work with messages on our phone. This module has the Inbox class of the same name, which allows you to read the message, sender and time. Each message has its own ID. A list of IDs of all messages can be obtained by the sms_messages () function. But we are more interested in the bind function, by which we specify a function that will be called immediately as soon as we receive a new message.
It will look like this:
- #coding: utf-8
- import e32
- from appuifw import app
- import inbox
- class Main:
- def incoming_callback (self, id):
- print 'incoming message'
- # make a short pause so that there are no conflicts
- # with the Messages app
- e32.ao_sleep (1)
- #create a new instance of the Inbox class, as in the old
- # new messages will not be visible
- messages = inbox.Inbox ()
- # we get the content of the message and the address where it came from
- print messages.content (id)
- print messages.address (id)
- # and delete it so that it does not interfere with us
- messages.delete (id)
- def __init __ (self):
- # create an instance of the inbox class and bind to the incoming function
- self.inb = inbox.Inbox ()
- self.inb.bind (self.incoming_callback)
- print 'start'
- lock = e32.Ao_lock ()
- app.exit_key_handler = lock.signal
- a = Main ()
- lock.wait ()
I would like to mention the features of the address in the message. If we received a message whose sender’s number is in the phone’s notebook, then it will return a name, not a number, to address. If there is no subscriber and notebook, then we will get a line with a number.
Work with the database
Through python, it is possible to work with the native symbian database, dbms, which is a relational database and supports sql syntax. I had to work 600-800 thousand records on the relatively old smartphone nokia 3230, which pretty well coped with this amount of data.
There are 2 modules for working with e32db databases, which works directly with the database and e32dbm, which is an add-on for e32db and implements a key-value database interface. Despite the fact that in this case we will use e32dbm, I would like to show how to work with e32db. Those who write in python are probably used to DB API 2, so the principles of working with this database will seem a bit strange.
E32db has 2 classes. One for writing to the database, the other for reading.
Creating a database, creating tables and filling them out looks like this:
- import e32db
- db_path = u'e: \\ data \\ python \\ test.db '
- db = e32db.Dbms ()
- db.create (db_path)
- db.open (db_path)
- db.execute (u'CREATE TABLE users (number CHAR (12), login LONG VARCHAR, password LONG VARCHAR) ')
- db.execute (u "" "INSERT INTO users VALUES ('+ 79xxxxxxxxx', 'kAIST', 'password')" "")
Do not forget that the path to the database and queries must be in Unicode.
Reading from a database is a bit more complicated.
- db_view = e32db.Db_view ()
- # make a request indicating an already open database
- db_view.prepare (db, u'SELECT * from users')
- # go through all the lines
- for x in xrange (db_view.count_line ()):
- # prepare a new line for output
- db_view.get_line ()
- # and look at what we have in the columns
- # strange, but the numbering starts with 1 and not with 0
- print db_view.col (1), db_view.col (2)
- # go to the next line
- db_view.next_line ()
For the application to work, e32dbm is enough for us, working with which is as simple as possible:
- import e32dbm
- db = e32dbm.open (u'e: \\ data \\ python \\ test ',' c ')
- db ['first'] = 'hello'
- db ['second'] = 'second key'
- db.sync ()
- db.close ()
Let's go directly to our task.
Let's take as a basis our code, which receives SMS messages. Add the following lines to the beginning:
- import e32dbm
- db = e32dbm.open (u'e: \\ mydb ',' c ')
It remains to write a function that will parse messages. In order for the subscriber to be able to work with this gate, his number must be associated with an account on Twitter. Let's not go through all possible authorization options and argue about security, but simply enter two commands: add login password and delete. The first command sent to the phone will add the username and password associated with the phone number to the database, and the second will be deleted. Accordingly, after such authorization, the user can simply write to the SMS gate number, which will go to Twitter.
- def parse (self, content, address):
- if content.startswith ('add'):
- print 'Add:', address
- db [address] = repr (content.split () [1: 3])
- db.sync ()
- elif content.startswith ('delete'):
- if address in db.keys ():
- print 'Delete:', adress
- del db [address]
- db.sync ()
- else:
- if address in db.keys ():
- print 'Send:', address
- self.send_twit (db [address], content)
and add the line to the incoming_callback function:
self.parse (messages.content (id), str (hash (messages.address (id))))
And it remains to implement the addition of a tweet, as we did in this topic.
def send_twit (self, tw, content):
login, password = eval (tw)
api = tweepy.API.new ('basic', login, password)
api.update_status (content)
Now our gate is ready. Optionally, it can be packaged in sis and filed in terms of error handling.
We take the source code here , and the ready sis is here.
Fresh ideas to you!