Created
April 22, 2019 12:48
-
-
Save jirawatee/267f6b3999dd253a1fe9c73ec51b0764 to your computer and use it in GitHub Desktop.
Scheduling a function in Cloud Functions for Firebase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require("firebase-functions"); | |
const request = require("request-promise"); | |
const OPENWEATHER_APPID = "<YOUR-OPEN-WEATHER-APP-ID>"; | |
const LINE_MESSAGING_API = "https://api.line.me/v2/bot/message"; | |
const LINE_UID = "<YOUR-LINE-ID>"; | |
const LINE_HEADER = { | |
"Content-Type": "application/json", | |
Authorization: | |
"Bearer <YOUR-CHANNEL-ACCESS-TOKEN>" | |
}; | |
exports.scheduledFunction = functions.pubsub.schedule("* * * * *").timeZone('Asia/Bangkok').onRun((context) => { | |
return request({ | |
method: "GET", | |
uri: `https://api.openweathermap.org/data/2.5/weather?appid=${OPENWEATHER_APPID}&units=metric&type=accurate&zip=10330,th`, | |
json: true | |
}).then(response => { | |
const message = `City: ${response.name}\nTemperature: ${response.main.temp}`; | |
return request({ | |
method: "POST", | |
uri: `${LINE_MESSAGING_API}/push`, | |
headers: LINE_HEADER, | |
body: JSON.stringify({ | |
to: LINE_UID, | |
messages: [ | |
{ | |
type: "text", | |
text: message | |
} | |
] | |
}) | |
}).then(() => { | |
return console.info("Done"); | |
}).catch(error => { | |
return Promise.reject(error); | |
}); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment