Back to working on the project. I want to add simple db support, so when I get a request from Alexa, I’ll check if the user is new or returned. If new – store the uuid in the db. If returning, give a returned user message.
Later I can also add context to the db, so I can manage contexts and states not only within session, but also accross sessions.
So, connecting to db – that’s something I did in the “My Clipboard” skill. I’ll go and look there how I did it.
Ok, I copied the set of functions from the clipboard app, but I now get an error when trying to call from Alexa – the entries table isn’t exsit… I remember that I had to do some manual creation of tables, but I don’t recall what exactly I needed to do.
Oh, I see what I’ve missed. I needed to create a schema.sql file, that defines the table I would like to create.
Here’s an article that is a good reference: Intro to Flask: Creating and Writing to Databases
And by the way, I’m moving to use Sublime for now… I spend too much time tinkering with Emacs, and not doing productive work… I will keep writing this in Emacs though, because it’s an org file.
Ok, back to Emacs. I guesse I’m more used to it than I thought…
I added the db support. I now want to check if a user is new or return. First time I see a user, I add the user to the db. Then I fetch from the db to check new or return. I had a problem with checking if I get a row from the db. Used this stack-overflow answer. The gist:
- Excecute the query with count(*)
- Grab the fetchone() with index 0
- Test if I get 1 or 0. If 0, then there was no result from the query.
@ask.launch def launched(): # check if the user is new or return db = get_db() c = db.execute('select count(*) from users where user_id = ?', [format(session.user.userId)]) data = c.fetchone() if data == 0: # new user - insert to db and play new user welcome db.execute('insert into users (user_id) values (?)', [format(session.user.userId)]) db.commit() return question('Welcome to out brain! I will be your personal editor. To start, say headlines, sites, topics or categories') else: return question('Welcome back! What do topic are you interested in today?')