About text recognition(twitter autoreply)

Hi all,
It me again still in the progress of my twitter project. Currently I’ve successfully blink up led via the function of twitter streaming. like below.
twitter.stream("@myname", onTweet);
My further stage gonna be let the imp able to recognize the text I’ve twit in order to running different function and twit me reply.
Does anyone have any ideas?

An reference I’ve got from github which is a PHP code, as a beginner I cant completely know all the words.

Sincerely seek for help.

This is a very open ended question - and there are a lot of possible answers. What specifically are you trying to accomplish?

Do you want to be able to recognize a set of specific commands (like ‘hallway lights on’, ‘open garage’, etc), or something a bit more generic?

@beardedinventor that’s exactly what I want. Furthermore, I wanna get information like asking" what the temperature right now" and get "Its 36 degree "as a answer. Does it make sense and possible to accomplish?

The easiest way to do this is to look for a certain set of known commands (there are much better ways, and more complex ways - but that’s a whole subject unto itself).

Here’s a really simple example where we look for certain keyword pairs to determin what functionality we want to invoke:

`// search for #RobotFriend
twitter.stream("#RobotFriend", function(tweet) {
local text = tweet.text.tolower();

if (text.find("light on") != null) {
    device.send("light", 1);
    twitter.tweet("I turned the light on (at " + time() + ")");
if (text.find("light off") != null) {
    device.send("light", 0);
    twitter.tweet("I turned the light off (at " + time() + ")");
if (text.find("get temp") != null || text.find("get the temp") != null) {
    twitter.tweet("The current temp (at " + time() + ") is " + temp);


This will let you tweet things like “#RobotFriend, please turn the light on” or “#RobotFriend - can you please get the temperature” …

If you want to do more complex things like “set temp 33.6” it’s a bit more complex, and I’ll let you try to figure that out (it probably involves breaking the text up by spaces, and parsing the words / parameters).

@beardedinventor really appreciate for your code. It deeply inspire and truly turn me on to keep developing my project. It should take me sometimes to digest. Really thank you and the whole community!

No problem! If get stuck down the line come ask more questions - but I encourage you take a stab at it, and find other people on the Internet who have done similar things before :slight_smile:

@beardedinventor , Thank you sooooo much !!
Can you recommend me some awesome site or source to start with?

The string.split is a good starting point.

I’m not sure where to you point you for more information or tutorials. You want to look for things like “text processing” and “building command parsers” probably?

Thanks for the string.split which is really useful.
Can I ask how to modify the code time() to let it shows the correct time into hour and minute ?

To work with time() in a more meaningful way, you create a new date object with the timestamp, then access it’s properties:

`local t = time();
// … stuff

local d = date(t);
server.log(d.sec); // seconds after the minute (0-59)
server.log(d.min); // minutes after the hour (0-59)
server.log(d.hour); // hours since midnight (0-23)
server.log(d.day); // day of the month (1-31)
server.log(d.month); // month (0-11, January = 0)
server.log(d.year); // year
server.log(d.wday); // day of the week (0-6, sunday = 0)
server.log(d.yday); // day of the year (0-365, Jan 1 = 0)`

More info about date here