Created
April 3, 2018 09:18
-
-
Save msojda/1bab13d8c5cef91caf58721a1be757c2 to your computer and use it in GitHub Desktop.
Giphy chat bot
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
const express = require("express"); | |
const app = express(); | |
const PORT = process.env.PORT || 3000; | |
const bodyParser = require("body-parser"); | |
const fetch = require("node-fetch"); | |
app.use(bodyParser.json()); | |
app.get("/", (req, res) => { | |
res.send("Hello World"); | |
}); | |
app.post("/top-secret-bot", (req, res) => { | |
const { space, type, message } = req.body || {}; | |
if (type === "ADDED_TO_SPACE" && space.type === "ROOM") { | |
res.send({ text: `Thanks for adding me to ${space.displayName}` }); | |
} else if (type === "MESSAGE") { | |
fetch( | |
`https://api.giphy.com/v1/gifs/random?api_key=YOUR_GIPHY_API_KEY&tag=${ | |
message.text | |
}&rating=G` | |
) | |
.then(response => response.json()) | |
.then(json => | |
res.send({ | |
cards: [ | |
{ | |
sections: [ | |
{ | |
widgets: [ | |
{ | |
image: { | |
imageUrl: json.data.images.fixed_height_small.url | |
} | |
}, | |
{ | |
buttons: [ | |
{ | |
textButton: { | |
text: "View on GIPHY", | |
onClick: { | |
openLink: { | |
url: json.data.url | |
} | |
} | |
} | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
] | |
}) | |
); | |
} | |
}); | |
app.listen(PORT, () => { | |
console.log(`listening on ${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment