-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request linedevth#3 from linedevth/master
Organize multiple functions following by modularization concept
- Loading branch information
Showing
10 changed files
with
661 additions
and
97 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rules": { | ||
".read": true, | ||
".write": true | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,28 @@ | ||
{ | ||
"functions": { | ||
"predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint"], | ||
"predeploy": [ | ||
"npm --prefix \"$RESOURCE_DIR\" run lint" | ||
], | ||
"source": "functions" | ||
}, | ||
"database": { | ||
"rules": "database.rules" | ||
}, | ||
"firestore": { | ||
"rules": "firestore.rules" | ||
}, | ||
"emulators": { | ||
"functions": { | ||
"port": 5001 | ||
}, | ||
"firestore": { | ||
"port": 8080 | ||
}, | ||
"database": { | ||
"port": 9000 | ||
}, | ||
"ui": { | ||
"enabled": true | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
service cloud.firestore { | ||
match /databases/{database}/documents { | ||
match /{document=**} { | ||
allow read, write; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,29 @@ | ||
const request = require("request-promise") | ||
const functions = require("firebase-functions") | ||
const LINE_MESSAGING_API = "https://api.line.me/v2/bot" | ||
const LINE_USER_ID = "YOUR-LINE-USER-ID" | ||
const LINE_HEADER = { | ||
"Content-Type": "application/json", | ||
Authorization: "Bearer YOUR-LINE-CHANNEL-ACCESS-TOKEN" | ||
} | ||
const OPENWEATHER_API = "https://api.openweathermap.org/data/2.5/weather/" | ||
const OPENWEATHER_APPID = "YOUR-OPEN-WEATHER-APP-ID" | ||
const util = require('./util') | ||
const runtimeOpts = { timeoutSeconds: 8, memory: "4GB" } | ||
const region = "asia-southeast2" | ||
|
||
exports.LineBotReply = functions.https.onRequest((req, res) => { | ||
exports.LineProxy = functions.region(region).runWith(runtimeOpts).https.onRequest(async (req, res) => { | ||
if (req.method === "POST") { | ||
reply(req.body) | ||
if (!util.verifySignature(req.headers['x-line-signature'], req.body)) { | ||
return res.status(401).send('Unauthorized') | ||
} | ||
|
||
let event = req.body.events[0] | ||
|
||
if (event.message !== undefined) { | ||
if (event.message.type !== "text") { | ||
util.reply(event.replyToken, { type: "text", text: JSON.stringify(event) }) | ||
} else { | ||
util.postToDialogflow(req) | ||
} | ||
} | ||
} | ||
return res.status(200).send(req.method) | ||
}) | ||
|
||
const reply = bodyResponse => { | ||
return request.post({ | ||
uri: `${LINE_MESSAGING_API}/message/reply`, | ||
headers: LINE_HEADER, | ||
body: JSON.stringify({ | ||
replyToken: bodyResponse.events[0].replyToken, | ||
messages: [{ type: "text", text: JSON.stringify(bodyResponse) }] | ||
}) | ||
}) | ||
} | ||
|
||
exports.LineBotPush = functions.https.onRequest(async (req, res) => { | ||
let response = await request.get({ | ||
uri: `${OPENWEATHER_API}?appid=${OPENWEATHER_APPID}&units=metric&type=accurate&zip=10330,th`, | ||
json: true | ||
}) | ||
const message = `City: ${response.name}\nWeather: ${response.weather[0].description}\nTemperature: ${response.main.temp}` | ||
return push(LINE_USER_ID, message) | ||
}) | ||
|
||
const push = async (targetId, msg) => { | ||
await request.post({ | ||
uri: `${LINE_MESSAGING_API}/message/push`, | ||
headers: LINE_HEADER, | ||
body: JSON.stringify({ | ||
to: targetId, | ||
messages: [{ type: "text", text: msg }] | ||
}) | ||
}) | ||
return res.status(200).send({ message: `Push: ${msg}` }) | ||
} | ||
|
||
exports.LineBotMulticast = functions.https.onRequest((req, res) => { | ||
const text = req.query.text | ||
if (text !== undefined && text.trim() !== "") { | ||
return multicast(res, text) | ||
} else { | ||
return res.status(400).send({ message: "Text not found" }) | ||
} | ||
}) | ||
|
||
const multicast = async (res, msg) => { | ||
await request.post({ | ||
uri: `${LINE_MESSAGING_API}/message/multicast`, | ||
headers: LINE_HEADER, | ||
body: JSON.stringify({ | ||
to: [LINE_USER_ID, "Ua0e8dd654eeb56790bc0e342bfd46e1c"], | ||
messages: [{ type: "text", text: msg }] | ||
}) | ||
}) | ||
return res.status(200).send({ message: `Multicast: ${msg}` }) | ||
} | ||
|
||
exports.LineBotBroadcast = functions.https.onRequest((req, res) => { | ||
const text = req.query.text | ||
if (text !== undefined && text.trim() !== "") { | ||
return broadcast(res, text) | ||
} else { | ||
const ret = { message: "Text not found" } | ||
return res.status(400).send(ret) | ||
} | ||
}) | ||
|
||
const broadcast = async (res, msg) => { | ||
await request.post({ | ||
uri: `${LINE_MESSAGING_API}/message/broadcast`, | ||
headers: LINE_HEADER, | ||
body: JSON.stringify({ | ||
messages: [{ type: "text", text: msg }] | ||
}) | ||
}) | ||
return res.status(200).send({ message: `Broadcast: ${msg}` }) | ||
} | ||
const line = require('./line') | ||
exports.lineBotReply = line.botReply | ||
exports.lineBotPush = line.botPush | ||
exports.lineBotMulticast = line.botMulticast | ||
exports.lineBotBroadcast = line.botBroadcast |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"use strict" | ||
|
||
const functions = require("firebase-functions") | ||
const axios = require('axios') | ||
const util = require('./util') | ||
|
||
const runtimeOpts = { timeoutSeconds: 8, memory: "4GB" } | ||
const region = "asia-southeast2" | ||
const LINE_USER_ID = functions.config().jirawatee.userid | ||
const OPENWEATHER_API = "https://api.openweathermap.org/data/2.5/weather/" | ||
const OPENWEATHER_APPID = functions.config().jirawatee.openweather_appid | ||
|
||
exports.botReply = functions.region(region).runWith(runtimeOpts).https.onRequest((req, res) => { | ||
if (req.method === "POST") { | ||
util.reply( | ||
req.body.events[0].replyToken, | ||
{ type: "text", text: JSON.stringify(req.body) } | ||
) | ||
} | ||
return res.status(200).send(req.method) | ||
}) | ||
|
||
exports.botPush = functions.region(region).runWith(runtimeOpts).https.onRequest(async (req, res) => { | ||
const response = await axios({ | ||
method: 'get', | ||
url: `${OPENWEATHER_API}?appid=${OPENWEATHER_APPID}&units=metric&type=accurate&zip=10330,th` | ||
}) | ||
const msg = `City: ${response.data.name}\nWeather: ${response.data.weather[0].description}\nTemperature: ${response.data.main.temp}` | ||
util.push(LINE_USER_ID, { type: "text", text: msg }) | ||
return res.status(200).send({ message: `Push: ${msg}` }) | ||
}) | ||
|
||
exports.botMulticast = functions.region(region).runWith(runtimeOpts).https.onRequest((req, res) => { | ||
const msg = req.query.msg | ||
if (msg !== undefined && msg.trim() !== "") { | ||
util.multicast({ type: "text", text: msg }) | ||
return res.status(200).send({ message: `Multicast: ${msg}` }) | ||
} else { | ||
return res.status(400).send({ message: "Text not found" }) | ||
} | ||
}) | ||
|
||
exports.botBroadcast = functions.region(region).runWith(runtimeOpts).https.onRequest((req, res) => { | ||
const msg = req.query.msg | ||
if (msg !== undefined && msg.trim() !== "") { | ||
util.broadcast({ type: "text", text: msg }) | ||
return res.status(200).send({ message: `Broadcast: ${msg}` }) | ||
} else { | ||
const ret = { message: "Text not found" } | ||
return res.status(400).send(ret) | ||
} | ||
}) |
This file contains 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
Oops, something went wrong.