Skip to content

Instantly share code, notes, and snippets.

@ternbusty
Last active February 19, 2018 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ternbusty/ae417abb6caa8ecf45454fb338ad7224 to your computer and use it in GitHub Desktop.
Save ternbusty/ae417abb6caa8ecf45454fb338ad7224 to your computer and use it in GitHub Desktop.
新着メールから天気予報取得し毎朝ツイートするtwitter bot (Google App Script)
function setTrigger() {
var triggerDay = new Date();
triggerDay.setHours(6);
triggerDay.setMinutes(30);
ScriptApp.newTrigger("main").timeBased().at(triggerDay).create();
}
// その日のトリガーを削除する関数(消さないと残る)
function deleteTrigger() {
var triggers = ScriptApp.getProjectTriggers();
for(var i=0; i < triggers.length; i++) {
if (triggers[i].getHandlerFunction() == "main") {
ScriptApp.deleteTrigger(triggers[i]);
}
}
}
function main() {
deleteTrigger();
var strTerms = '("ウェザーニュース朝刊")';
var myThread = GmailApp.search(strTerms, 0, 1);
var myMessage = GmailApp.getMessagesForThreads(myThread);
var content = myMessage[0][0].getPlainBody();
var arrayOfStrings = content.split(/\r?\n/g);
var tweet = arrayOfStrings[2] + "\n" + arrayOfStrings[4] + arrayOfStrings[5] + "\n" + arrayOfStrings[7];
// Logger.log(tweet)
postTweet(tweet);
if (arrayOfStrings[2].match(/雪/)) {
changeIcon("http://www.emoji.co.uk/files/twitter-emojis/animals-nature-twitter/10763-snowflake.png")
lineNotify();
} else if (arrayOfStrings[2].match(/雨/)){
changeIcon("http://www.emoji.co.uk/files/twitter-emojis/animals-nature-twitter/10766-umbrella-with-rain-drops.png")
lineNotify();
} else if (arrayOfStrings[2].match(/曇り/)){
changeIcon("https://www.fastpic.jp/images.php?file=7165332978.png")
} else {
changeIcon("http://www.emoji.co.uk/files/twitter-emojis/animals-nature-twitter/10757-black-sun-with-rays.png")
}
}
var CONSUMER_KEY = '...';
var CONSUMER_SECRET = '...';
/**
* Authorizes and makes a request to the Twitter API.
*/
function postTweet(tweet) {
var service = getService();
if (service.hasAccess()) {
var url = 'https://api.twitter.com/1.1/statuses/update.json';
var payload = {
status: tweet
};
var response = service.fetch(url, {
method: 'post',
payload: payload
});
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
} else {
var authorizationUrl = service.authorize();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
}
}
function changeIcon(url) {
var service = getService();
// var image = DriveApp.getFileById('1wB246j2qEZPmq_X97mTNOIlFSHRNba9h');
var getImage = UrlFetchApp.fetch(url);
var image = getImage.getBlob();
var resp_64 = Utilities.base64Encode(image.getBytes());//TwitterAPI送信用にbase64にエンコード
if (service.hasAccess()) {
var url = 'https://api.twitter.com/1.1/account/update_profile_image.json';
var payload = {
image: resp_64
};
var response = service.fetch(url, {
method: 'post',
payload: payload
});
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
} else {
var authorizationUrl = service.authorize();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
}
}
/**
* Reset the authorization state, so that it can be re-tested.
*/
function reset() {
var service = getService();
service.reset();
}
/**
* Configures the service.
*/
function getService() {
return OAuth1.createService('Twitter')
// Set the endpoint URLs.
.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
.setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
.setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
// Set the consumer key and secret.
.setConsumerKey(CONSUMER_KEY)
.setConsumerSecret(CONSUMER_SECRET)
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties());
}
/**
* Handles the OAuth callback.
*/
function authCallback(request) {
var service = getService();
var authorized = service.handleCallback(request);
if (authorized) {
return HtmlService.createHtmlOutput('Success!');
} else {
return HtmlService.createHtmlOutput('Denied');
}
}
function sendHttpPost(message){
var token = "...";
var options =
{
"method" : "post",
"payload" : "message=" + message,
"headers" : {"Authorization" : "Bearer "+ token}
};
UrlFetchApp.fetch("https://notify-api.line.me/api/notify",options);
}
function lineNotify(){
var message="Take your umbrella with you!" ;
sendHttpPost(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment