Skip to content

Instantly share code, notes, and snippets.

@msojda
Created April 3, 2018 09:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save msojda/1bab13d8c5cef91caf58721a1be757c2 to your computer and use it in GitHub Desktop.
Save msojda/1bab13d8c5cef91caf58721a1be757c2 to your computer and use it in GitHub Desktop.
Giphy chat bot
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