Skip to content

Instantly share code, notes, and snippets.

@hiiamyes
Created August 21, 2019 07:05
Show Gist options
  • Save hiiamyes/a71a5f39831ea240dabbda438531d1bd to your computer and use it in GitHub Desktop.
Save hiiamyes/a71a5f39831ea240dabbda438531d1bd to your computer and use it in GitHub Desktop.
backend-cookbook-2-server.js
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const { WebClient } = require("@slack/web-api");
const getWeather = require("./getWeather");
const slackWebClient = new WebClient(process.env.SLACK_BOT_API_TOKEN);
const app = express();
app.use(bodyParser.json());
app.post("/", async function(request, response) {
try {
const {
event: { type, subtype, text, channel }
} = request.body;
if (
text &&
text.includes(`<@${process.env.SLACK_BOT_ID}`) &&
request.headers["x-slack-retry-num"] === undefined
) {
await slackWebClient.chat.postMessage({
channel,
parse: "full",
attachments: [
{
fallback: "6-06",
image_url: `https://www.cwb.gov.tw/Data/fcst_img/QPF_ChFcstPrecip_6_06.png?ts=${new Date().getTime()}`
},
{
fallback: "6-12",
image_url: `https://www.cwb.gov.tw/Data/fcst_img/QPF_ChFcstPrecip_6_12.png?ts=${new Date().getTime()}`
},
{
fallback: "6-18",
image_url: `https://www.cwb.gov.tw/Data/fcst_img/QPF_ChFcstPrecip_6_18.png?ts=${new Date().getTime()}`
},
{
fallback: "6-24",
image_url: `https://www.cwb.gov.tw/Data/fcst_img/QPF_ChFcstPrecip_6_24.png?ts=${new Date().getTime()}`
}
]
});
}
} catch (err) {
console.log(err);
} finally {
response.send(request.body.challenge);
}
});
const listener = app.listen(process.env.PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment