Lua to retrieve Google unread email count

I’d like to perform a TTS… “You Have new Email” for my wife when she is home.

Has anyone used Lua to retrieve the quantity of unread emails from a google account?

I tried on my OSX box with applescript, but I get timeout errors. I have a huge inbox and it just takes to long to cycle through them.

I’m hoping for a shortcut…

Thats cool… I was looking to do the same but email from a specific source

(In our office I have a SONOS speaker and want a TTS to say there is a new voicemail waiting"

So far I got nothing either… so hope you fig. something out that I can ride your coat tails on :slight_smile:

OK, I created a URL that will return my unread email quantity using googlescript and this gentleman’s solution:

This gives me a URL to call, which when I go to that URL, it returns the following string:

0 There are 0 unread messages in my Gmail Inbox. (when empty) &
15 There are 15 unread messages in my Gmail Inbox. (when non empty)

I am trying to use wget to grab that string, test the first character and look for the zero, but I know luup.inet.wget returns two variables, I am just struggling with how to get the string portion. It looks like it is returning the return status successful number {0} OK. So I just need to figure out how to grab that string. I’m now guessing it is preceded by some html instructions, but I’m out of my zone there.

local htmstatus, mailcount = luup.inet.wget("https://script.google.com/macros/s/AKfycbyu8TRKPQzJwlYYTcRCpO6b2zShRJ686G846pxG28v2vsvOQoDX/exec")
count = string.sub(mailcount,1,1)
if count == "0"
     then
     luup.variable_set("urn:upnp-org:serviceId:VSwitch1","Status", 0 ,10)
     else
     luup.variable_set("urn:upnp-org:serviceId:VSwitch1","Status", 1 ,10)
end

I need a little help with luup.inet.wget.

When you get this all figured out it would be GREAT if you could patch the gcal plugin with it. The Vera implementation of the Google services is woefully pathetic compared to what HomeSeer’s got. I’ve got 1/2 a mind to run HomeSeer just for the Google integration. Now that PLEG exists that is…

[quote=“Bulldoglowell, post:3, topic:180402”]OK, I created a URL that will return my unread email quantity using googlescript and this gentleman’s solution:

This gives me a URL to call, which when I go to that URL, it returns the following string:

0 There are 0 unread messages in my Gmail Inbox. (when empty) &
15 There are 15 unread messages in my Gmail Inbox. (when non empty)

I am trying to use wget to grab that string, test the first character and look for the zero, but I know luup.inet.wget returns two variables, I am just struggling with how to get the string portion. It looks like it is returning the return status successful number {0}, meaning that the following code will only return an OFF to my Virtual Switch.

local htmstatus, mailcount = luup.inet.wget("https://script.google.com/macros/s/AKfycbyu8TRKPQzJwlYYTcRCpO6b2zShRJ686G846pxG28v2vsvOQoDX/exec")
count = string.sub(mailcount,1,1)
if count == "0"
     then
     luup.variable_set("urn:upnp-org:serviceId:VSwitch1","Status", 0 ,10)
     else
     luup.variable_set("urn:upnp-org:serviceId:VSwitch1","Status", 1 ,10)
end

I need a little help with luup.inet.wget.[/quote]

If wget returns numerical zero in the first variable, the request was successful. You should be able to extract the count from the string in the second variable. If it doesn’t work, you will need to check the exact format of the string. It could be in json format or with some xml tags. May I humbly suggest that LuaTest would help you out? If you added a few print statements you would soon see what you were getting back.

local htmstatus, mailcount = luup.inet.wget("https://script.google.com/macros/s/AKfycbyu8TRKPQzJwlYYTcRCpO6b2zShRJ686G846pxG28v2vsvOQoDX/exec") print("htmstatus: " .. htmstatus) print("mailcount:" .. mailcount) count = string.sub(mailcount,1,1) print("count: " .. count) if count == "0" then luup.variable_set("urn:upnp-org:serviceId:VSwitch1","Status", 0 ,10) else luup.variable_set("urn:upnp-org:serviceId:VSwitch1","Status", 1 ,10) end

If htmstatus returns non-zero, then there is either something wrong with your url or it could have timed-out. wget has a default timeout of five seconds. You can change this by appending ,10 or whatever to the wget call arguments.

thanks rex, I’ll try that.
Appreciate the help.

added: It is not timing out, because I can dump htmstatus into a variable container.

[quote=“RexBeckett, post:5, topic:180402”]If wget returns numerical zero in the first variable, the request was successful. You should be able to extract the count from the string in the second variable. If it doesn’t work, you will need to check the exact format of the string. It could be in json format or with some xml tags. May I humbly suggest that LuaTest would help you out? If you added a few print statements you would soon see what you were getting back.

If htmstatus returns non-zero, then there is either something wrong with your url or it could have timed-out. wget has a default timeout of five seconds. You can change this by appending ,10 or whatever to the wget call arguments.[/quote]

[code]LuaTest 1.5

Lua file: /etc/cmh-ludl/luatest.lua

Results
No errors
Runtime: 582.3 ms
Code returned: nil

Print output
Start
htmstatus= 0
mailcount string length= 515
countone= REF=“https://script.googleusercontent.com/macros/echo?user_content_key=BQEbZNdKFq_vBRYesl8U7dtKPWjSHtRiwfIfLE2IBWFqUBUDPUUgiH3_mX3DiVQWcvAGh-87Ozy6kboSpTc_T8VTc2j0Q_Wbm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnM8wVweBD8j5M9TPhW4kqeSafcnCzjoG07v4lw0zzd9rldvFNeEp58UfzyhAVN2T4AkO-9SPrbMN&lib=MS02ODZy_-OT8h9i_52b0_bI9HD-39cmB”>here.

countnumber= HTML

Code
1 print (“Start”)
2 local htmstatus, mailcount = luup.inet.wget(“https://script.google.com/macros/s/AKfycbwrdyjsIR-OnnooP6AJVw_1gSxBrTi8AdAXSRJNGxLh7pTJmigT/exec”)
3 print (“htmstatus=”, htmstatus)
4 strlength=string.len(mailcount)
5 print(“mailcount string length=”,strlength)
6 --print (mailcount)
7 countone = string.sub(mailcount, 150, 515)
8 print (“countone=”,countone)
9 countnumber = string.match(mailcount, “(%a+)%s*”)
10 print (“countnumber=”, countnumber)
11 --luup.variable_set(“urn:upnp-org:serviceId:VContainer1”,“Variable5”,countnumber,15)
12 --if count == “0”
13 – then
14 – luup.variable_set(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 0 ,10)
15 – else
16 – luup.variable_set(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 1 ,10)
17 --end
[/code]

Rex,

In trying to see what’s in mail count, I wasn’t able to print it out. It is apparently getting fed into your debug routine and running. I snooped a bit and realized that this is a big string (515char) and there are pieces of it that I cannot print.

It looks almost exactly like what appears in my browsers search box after I hit this url:

https://script.google.com/macros/s/AKfycbwrdyjsIR-OnnooP6AJVw_1gSxBrTi8AdAXSRJNGxLh7pTJmigT/exec

So, I’m not getting the content of the output, rather just some link.

Any idea what’s happening here?

Why does the gcal plug-in need to be patched?

  • Garrett

[quote=“garrettwp, post:8, topic:180402”]Why does the gcal plug-in need to be patched?

  • Garrett[/quote]

It isn’t that it doesn’t do what it does well. In my experience it’s pretty solid. It just could do a LOT more. Off the top of my head I can name an often requested feature: see future events.

But this is just scratching the surface of what’s possible.

As always I’m grateful for what the community has given and without the contribution of those more able than myself Vera would be much less capable. But. If there is additional capability to be added I heartily welcome it.

Have you seen the HomeSeer Google services plugin?

If you were to then ask “Why don’t you use HomeSeer if it’s so much better?” I would have to say that it’s because of the remote connection framework provided by MCV and AutHomation. The usability of AutHomation compared to the HomeSeer offerings is, also, night and day.

Rex,

It seems that :

Redirects- For security reasons, content returned by the Content service isn't served from script.google.com, but instead redirected to a one-time URL at script.googleusercontent.com. This means that if you use the Content service to return data to another application, you must ensure that the HTTP client is configured to follow redirects. For example, in the cURL command line utility, add the flag -L. Check the documentation for your HTTP client for more information on how to enable this behavior..

So, i have to find the right output form to get what I want; a content string or file, probably.

I’ll let you know…

Google Script

[quote=“jimpapa, post:2, topic:180402”]Thats cool… I was looking to do the same but email from a specific source

(In our office I have a SONOS speaker and want a TTS to say there is a new voicemail waiting"

So far I got nothing either… so hope you fig. something out that I can ride your coat tails on :)[/quote]

@jimpapa

I tried to use Google Script to return unread email count, to no avail. It seems that there is some security built in that redirects the url and then things get wonky with luup.inet.wget. Rex even tried to help but I gave up when we hit the Google security issue and couldn’t retrieve a simple single character from the script output.

I didn’t want to use my server, trying not to create too much complexity and reliance in my mac, which has to be running with mail open for this to work. Of course, you could always start mail with the applescript if it isn’t already running, but I usually have my admin user signed on hosting other services like iTunes, for example.

So, (and I don’t know if you have a mac) I introduced my OSX server to solve the problem. I’m running an applescript (for which I have great fondness) from a scene that goes every 2mins except after hours (thanks PLEG!). The bonus is that you can use any email account you have attached to your mac.

In the scene, I call this applescript to run:

os.execute("ssh -y -i ~/.ssh/id_rsa root@192.168.1.9 osascript /usr/bin/checkemailcount.txt")

The applescript is straightforward:

tell application "Mail" set mailcount to unread count of inbox end tell if mailcount > 0 then set mysource_html to do shell script "curl " & quoted form of "http://192.168.1.2:3480/data_request?id=variableset&DeviceNum=10&serviceId=urn:upnp-org:serviceId:VSwitch1&Variable=Status&Value=1" else set mysource_html to do shell script "curl " & quoted form of "http://192.168.1.2:3480/data_request?id=variableset&DeviceNum=10&serviceId=urn:upnp-org:serviceId:VSwitch1&Variable=Status&Value=0" end if

You could compile it and run it in a chron, but I like having Vera stay in charge of this because she’s the only one using the data. I’m toggling a virtual switch so that I can send the TTS “you have new email” but only when the toggle flips to ON (thanks PLEG).

I spent the time on this because I am building a ‘visual notification’ system with Arduino, lighting some LEDs with the toggle.

I’ll let you know how that goes.

I attached the applescript compiler view so that you can see the output…

That’s awesome. Yes, I’m all MAC here and in the office.

I’ll try to replicate what you did this week, thanks!

Great minds think alike :slight_smile:http://forum.micasaverde.com/index.php/topic,13805.msg103831.html#msg103831

So I’m watching this thread with much interest…

[quote=“parkerc, post:13, topic:180402”]Great minds think alike :slight_smile:http://forum.micasaverde.com/index.php/topic,13805.msg103831.html#msg103831

So I’m watching this thread with much interest…[/quote]

Wow, thanks for the link. I actually hadn’t put any thought into a POP3 or IMAP solution, I really just started this to get the TTS notification for my wife, who only uses Gmail.

I spent that time on it because it looked promising with google script, but you can see how that turned out. The applescript works well, so in a way, the problem is solved for me (albeit I really don’t like the extra jump through my server).

It would be good to create an email sensor plugin so that the rest of the world could easily do this; extracting the yes/no toggle and the number of unread emails would be 99% of what I could imagine needing.

I wonder if there is a lot of interest here for that.

thanks again, you have me thinking…

I’m glad to help with the envisioning phase :wink:

I too loved the idea of being told about new emails e.g TTS what the subject line is or maybe set triggers based on the email sender or the subject line e.g

"an email from ‘John’ has just arrived called ‘need your help’ " :frowning:

I’ve seen various bits of code in Perl online e.g Mail::Webmail::Gmail - An interface to Google's webmail service - metacpan.org, but I did not fancy installing all the necessary Perl files etc. I ideally wanted it to be more native.

For me, this would be a great integration (I’ve been waiting a while to see if someone could pull all the pieces together and get it to work…) :wink:

[quote=“S-F, post:9, topic:180402”][quote=“garrettwp, post:8, topic:180402”]Why does the gcal plug-in need to be patched?

  • Garrett[/quote]

It isn’t that it doesn’t do what it does well. In my experience it’s pretty solid. It just could do a LOT more. Off the top of my head I can name an often requested feature: see future events.[/quote]
@ S-F

I just stumbled across this post - have you taken a look at GCAL_II ? One of the features I added (among many) was a json encoded string that gives the next 24hrs of events. Does this do what you were envisaging ?