📜 ⬆️ ⬇️

SMS thermometer from a bunch of Google script, Google Doc, Wolfram-Alpha and Sms.ru

Hello!
I wanted to make sure that on my simple phone in the morning there were alerts about the current weather (of course, for free). Because there is no ordinary thermometer, because of this, how much warmth you need to dress is not always understandable.
Who cares how I implemented it - welcome under cat.

Looking for something like this on a habr, I stumbled upon this article by bakatrouble , from which I learned about the sms.ru service, about the Google script and so on, for which a special thank you.
I decided to receive weather information from Wolfram-Alpha. Why tungsten alpha? At one time, I was conquered by the line “the data was taken from the Balandino meteorological station, 119 km from the center of Zlatoust”. Therefore, I wanted to take information from this site.

So, first we need to create (if not) an account on Google, as well as register on the Sms.ru service (if, again, not). The Sms.ru service at the time of this writing provides 5 free messages to your number (on their website it says “for programmers”).

After successful registration, go to Google-Drive (drive.google.com) and create a new empty document. We give him a name and wait until it remains on the disk.
Now go to script.google.com, select the Blank Project. We give the future script some name (the script will also be saved on the Google disk). Remove everything that is there.
Now paste the following code:
')
Script code
function responseWeather() { // : ,     SMS,  ID  sms.ru,   Google var cityTown= "zlatoust"; var phoneNum="71111111111"; var apiID="111111111111111111111111111111111111"; var docID="11111111111111111111111111111111111111111111"; // ,        var serverAv = false; while (serverAv == false){ try { DocumentApp.openById(docID); serverAv=true; } catch (e) { Logger.log(' '); } } var docmain = DocumentApp.openById(docID); var bodydoc = docmain.getBody(); bodydoc.clear();//     var response = UrlFetchApp.fetch("http://www.wolframalpha.com/input/?i="+cityTown+"+weather"); bodydoc.setText(response); var textEdit = bodydoc.editAsText(); textEdit.deleteText(0, 37280); //  ,     /*      .      -  .     - m\s  mph . ,   . */ if (textEdit.findText('&deg;C')!=null){ var degree = '&deg;C'; var convert = false; } else { degree = '&deg;F'; convert = true; } //     textEdit.deleteText(0, textEdit.findText(degree).getStartOffset()-5); textEdit.replaceText('</span><span class="info"><dt></dt><dd class="conditions">', ', '); textEdit.replaceText('</dd><dt>', '; '); textEdit.replaceText('wind:</dt><dd>', 'wind: '); textEdit.replaceText('<span> at </span>', ' at '); textEdit.replaceText('humidity:</dt><dd>', 'hum.: '); textEdit.deleteText(textEdit.findText("%").getEndOffsetInclusive()+1, textEdit.findText('" />').getEndOffsetInclusive()); textEdit.replaceText('<span class="high">', ' (buf'); textEdit.replaceText('</span><span class="low">', '...fub'); textEdit.replaceText("</span>", ")</span>"); textEdit.deleteText(textEdit.findText("</span>").getStartOffset(), textEdit.getText().length-1); //       -     ,  if (textEdit.findText('>')!=null){ textEdit.deleteText(0, textEdit.findText('>').getEndOffsetInclusive()); } // if (convert==true){ //    var buffer1 = textEdit.getText(); textEdit.deleteText(textEdit.findText(degree).getStartOffset(), textEdit.getText().length-1); var len = textEdit.getText().length-1; var buffer2 = Number(textEdit.getText()); bodydoc.clear(); var result = Math.round((buffer2-32)/1.8); textEdit.setText(buffer1); textEdit.deleteText(0, len); textEdit.insertText(0, result); //    buffer1 = textEdit.getText(); textEdit.deleteText(0, textEdit.findText("buf").getEndOffsetInclusive()); textEdit.deleteText(textEdit.findText(degree).getStartOffset(), textEdit.getText().length-1); len = textEdit.getText().length-1; buffer2 = Number(textEdit.getText()); bodydoc.clear(); result = Math.round((buffer2-32)/1.8); textEdit.setText(buffer1); textEdit.deleteText(textEdit.findText("buf").getEndOffsetInclusive()+1, textEdit.findText("buf").getEndOffsetInclusive()+1+len); textEdit.insertText(textEdit.findText("buf").getEndOffsetInclusive()+1, result); //    buffer1 = textEdit.getText(); textEdit.deleteText(0, textEdit.findText('fub').getEndOffsetInclusive()); textEdit.deleteText(textEdit.findText(degree).getStartOffset(), textEdit.getText().length-1); len = textEdit.getText().length-1; buffer2 = Number(textEdit.getText()); bodydoc.clear(); result = Math.round((buffer2-32)/1.8); textEdit.setText(buffer1); textEdit.deleteText(textEdit.findText('fub').getEndOffsetInclusive()+1,textEdit.findText('fub').getEndOffsetInclusive()+1+len); textEdit.insertText(textEdit.findText('fub').getEndOffsetInclusive()+1, result); //   buffer1 = textEdit.getText(); textEdit.deleteText(0, textEdit.findText('at ').getEndOffsetInclusive()); textEdit.deleteText(textEdit.findText('mph').getStartOffset(), textEdit.getText().length-1); len = textEdit.getText().length-1; buffer2 = Number(textEdit.getText()); bodydoc.clear(); result = Math.round(buffer2*0.44704); textEdit.setText(buffer1); textEdit.deleteText(textEdit.findText('at ').getEndOffsetInclusive()+1,textEdit.findText('at ').getEndOffsetInclusive()+1+len); textEdit.insertText(textEdit.findText('at ').getEndOffsetInclusive()+1, result); textEdit.replaceText('mph', 'm/s'); }; //... textEdit.replaceText('buf', ''); textEdit.replaceText('fub', ''); textEdit.replaceText(degree, "*C"); //      textEdit.insertText(textEdit.getText().length, ' '+cityTown); //      ,    if (textEdit.getText().length>70){ textEdit.deleteText(69, textEdit.getText().length-1); } var textSMS = bodydoc.getText(); UrlFetchApp.fetch("http://sms.ru/sms/send?api_id="+apiID+"&to="+phoneNum+"&text="+encodeURI(textSMS)); bodydoc.clear(); } 



Insert the necessary data into the corresponding variables.
The variable cityTown is the city by translit for which you want to receive the weather; phoneNum - the number that you registered on Sms.ru and, accordingly, to which free SMS will be sent (without a plus and any separators); apiID is a unique identifier for the Sms.ru service, look at the corresponding site:


docID is a unique identifier of a document (not a script!) on a Google disk. You can find in the address bar:


After all the necessary variables are filled, run the script. When you first start the script will request permission to work with links and documents on Google-ROM. Allow everything. The script should work, and the phone will receive a message.
Now it remains to set the automatic start. To do this, click on the icon (1), select the desired interval (2).



Since I needed to know the weather early in the morning before going to work, I set the timer at 7 am. For myself, I also set alerts to the post office in the event of a code development error - in the “immediately” interval.
Save the trigger. Now everything is ready, every morning there will be a message about the current weather.

A few comments about the code. I know javascript superficially, and the google-script is even less - so, if you have any comments on optimization, I will be glad to know. Regarding the conversion from Fahrenheit to Celsius. Apparently, the location of the request is transmitted to Wolfram, and it adjusts the output data for the region. Therefore, when script debugging goes on-line (when starting manually), Google reports that I am from Russia, and Wolfram responds with Celsius and meters per second. When you start from the trigger - Google says I'm in California, which is why Wolfram gives Fahrenheit and miles per hour.

For the future, I also plan to make a weather request via MMS - by sending a message to the mailbox with the text “city weather” and receiving a reply message. Maybe a controversial decision, I just connected a long-term package and MMS is not very expensive.

I did hang the usual thermometer, because my wife insisted that it was necessary to verify the data received.

UPD 11/03/14: Slightly corrected the script, the Google Doc availability check loop did not work correctly (sometimes it returns an error when opening a document). Now everything should work more stable.

UPD 11/22/14: When setting the trigger time, consider which time zone Google uses. For example, I have before the transfer of hours, Google issued a time zone that coincides with mine (+6). Then, for interest, I deleted this trigger and set it off in a new way. This time I was given a different time zone, so I had to set the time according to it. In my case, the launch from 6 to 7 in the morning turned from 5 to 6 in the evening.


So be careful.

Source: https://habr.com/ru/post/240533/


All Articles