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 I received alerts about the current weather (of course, for free). Because there is no ordinary thermometer, because of this how much heat you need to dress, it is not always clear.
Who cares how I realized this - welcome to cat.
Having looked for something similar on a hub, I came across this article here due to the authorship of bakatrouble , from where I found out about the sms.ru service, about Google script and so on, for which special thanks.
I decided to get weather information from Wolfram-Alpha. Why is tungsten alpha? At one time, I was captivated by the line "data taken from the weather station Balandino, 119 km from the center of Zlatoust." Therefore, I wanted to take information from this site.
So, for starters, we need to create (if not) an account on Google, and also register on the Sms.ru service (if, again, not). Sms.ru service provides at the time of writing 5 free messages to your number (on their website it says "for programmers").
After successful registration, go to the Google drive (drive.google.com) and create a new blank document. We give him a name and wait for it to remain on disk.
Now go to script.google.com, select Blank Project. We give the future script some name (the script will also be saved on Google disk). We delete everything that is there.
Now insert the following code:
Insert the necessary data into the appropriate variables.
The cityTown variable is the transliteration city 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 plus and without any separators); apiID - a unique identifier for the Sms.ru service, look at the corresponding site:

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

After all the necessary variables are filled, run the script. At the first launch, the script will request permission to work with links and documents on Google disk. Allow all. The script should work, and a message will come to the phone.
Now it remains to set the automatic start. To do this, click on the icon (1), select the required interval (2).

Since I needed to know the weather early in the morning before going to work, I set the timer at 7 o’clock in the morning. For myself, I also set up notifications in the mail in case of an error in working out the code - in the interval “immediately”.
Save the trigger. Now everything is ready, every morning a message about the current weather will come.
A few notes about the code. I know javascript superficially, and google-script is even less - therefore, if there are any comments on optimization, I will be glad to know. Regarding the conversion from Fahrenheit to Celsius. Apparently, data on the location of the request is transmitted to Tungsten, and it adjusts the output data to the region. Therefore, when the script is debugged online (when manually launched), Google reports that I am from Russia, and Wolfram responds with Celsius and meters per second. When launched from a trigger, Google says I'm in California, which is why Wolfram gives out Fahrenheit and miles per hour.
For the future, I plan to make a weather request via MMS by sending a message to the mailbox with the text “city weather” and receiving a response message. Maybe a controversial decision, it’s just that I have a long-standing package and MMS are not very expensive.
I hung the usual thermometer, for thewife insisted it was necessary to verify the data received.
UPD 11/03/14: I corrected the script a bit, the Google Doc availability check loop didn’t 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, before transferring the clock, Google issued a time zone that coincided with mine (+6). Then, for fun, I deleted this trigger and set it in a new way. This time I was given a different time zone, so I had to set the time in accordance with it. In my case, the launch from 6 to 7 in the morning turned from 5 to 6 in the evening.

So be careful.
I wanted to make sure that on my simple phone in the morning I received alerts about the current weather (of course, for free). Because there is no ordinary thermometer, because of this how much heat you need to dress, it is not always clear.
Who cares how I realized this - welcome to cat.
Having looked for something similar on a hub, I came across this article here due to the authorship of bakatrouble , from where I found out about the sms.ru service, about Google script and so on, for which special thanks.
I decided to get weather information from Wolfram-Alpha. Why is tungsten alpha? At one time, I was captivated by the line "data taken from the weather station Balandino, 119 km from the center of Zlatoust." Therefore, I wanted to take information from this site.
So, for starters, we need to create (if not) an account on Google, and also register on the Sms.ru service (if, again, not). Sms.ru service provides at the time of writing 5 free messages to your number (on their website it says "for programmers").
After successful registration, go to the Google drive (drive.google.com) and create a new blank document. We give him a name and wait for it to remain on disk.
Now go to script.google.com, select Blank Project. We give the future script some name (the script will also be saved on Google disk). We delete everything that is there.
Now insert the following code:
Script code
functionresponseWeather()
{
//Здесь вписывается: город, телефонный номер для доставки SMS, уникальный ID с sms.ru, код документа Googlevar 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('°C')!=null){
var degree = '°C';
var convert = false;
} else {
degree = '°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 appropriate variables.
The cityTown variable is the transliteration city 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 plus and without any separators); apiID - a unique identifier for the Sms.ru service, look at the corresponding site:

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

After all the necessary variables are filled, run the script. At the first launch, the script will request permission to work with links and documents on Google disk. Allow all. The script should work, and a message will come to the phone.
Now it remains to set the automatic start. To do this, click on the icon (1), select the required interval (2).

Since I needed to know the weather early in the morning before going to work, I set the timer at 7 o’clock in the morning. For myself, I also set up notifications in the mail in case of an error in working out the code - in the interval “immediately”.
Save the trigger. Now everything is ready, every morning a message about the current weather will come.
A few notes about the code. I know javascript superficially, and google-script is even less - therefore, if there are any comments on optimization, I will be glad to know. Regarding the conversion from Fahrenheit to Celsius. Apparently, data on the location of the request is transmitted to Tungsten, and it adjusts the output data to the region. Therefore, when the script is debugged online (when manually launched), Google reports that I am from Russia, and Wolfram responds with Celsius and meters per second. When launched from a trigger, Google says I'm in California, which is why Wolfram gives out Fahrenheit and miles per hour.
For the future, I plan to make a weather request via MMS by sending a message to the mailbox with the text “city weather” and receiving a response message. Maybe a controversial decision, it’s just that I have a long-standing package and MMS are not very expensive.
I hung the usual thermometer, for the
UPD 11/03/14: I corrected the script a bit, the Google Doc availability check loop didn’t 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, before transferring the clock, Google issued a time zone that coincided with mine (+6). Then, for fun, I deleted this trigger and set it in a new way. This time I was given a different time zone, so I had to set the time in accordance with it. In my case, the launch from 6 to 7 in the morning turned from 5 to 6 in the evening.

So be careful.