Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //SendFurryFriend, Thomas Turner 27/02/19
  2.  
  3. //Dependancies
  4. var request = require("request");
  5. var express = require('express');
  6. var moment = require('moment');
  7. var app = express();
  8.  
  9. console.log(moment().format('lll'), '>>>cat-send 1.1 running');
  10. console.log(moment().format('lll'), '>>>Set to msg at 08:00');
  11.  
  12. /*
  13. API REQUEST & WEBHOOK
  14. */
  15.  
  16. function getCat() {
  17.   var options = {
  18.     method: 'GET',
  19.     url: 'https://api.thecatapi.com/v1/images/search',
  20.     headers:
  21.     {
  22.       'Token': 'b20162f6-21d8-4fc7-aede-20d74f9a1b51',
  23.       'cache-control': 'no-cache'
  24.     }
  25.   };
  26.  
  27.   request(options, function (error, response, body) {
  28.     if (error) throw new Error(error);
  29.     var catImg;
  30.  
  31.     console.log(moment().format('lll'), '>>>Body:', body);
  32.  
  33.     var jsonObject = JSON.parse(body);
  34.  
  35.     console.log(moment().format('lll'), '>>>URL:', jsonObject[0].url);
  36.    
  37.     catImg = jsonObject[0].url;
  38.     return catImg;
  39.   });
  40.   var catImg = catImg;
  41.   return catImg;
  42. };
  43.  
  44. function postToDiscord(discordWebhookURL, catImg){
  45.   //Sending to discord
  46.   var options = {
  47.     method: 'POST',
  48.     url: discordWebhookURL,
  49.     headers:
  50.     {
  51.       'Postman-Token': '166ba090-5cd7-48a4-8cae-4c856b5d8f49',
  52.       'cache-control': 'no-cache',
  53.       'Content-Type': 'application/json'
  54.     },
  55.     body:
  56.     {
  57.       content: 'Here\'s your cat! Fresh from one of Azure\'s Data Centres.',
  58.       embeds: [{ image: { url: catImg } }]
  59.     },
  60.     json: true
  61.   };
  62.   request(options, function (error, response, body) {
  63.     if (error) throw new Error(error);
  64.   });
  65. };
  66.  
  67. async function waitForCat(){
  68.   let catImg = await getCat();
  69.   return catImg;
  70. };
  71.  
  72. /*
  73. EXPRESS WEB SERVER
  74. */
  75.  
  76. app.set('view engine', 'ejs');
  77.  
  78. app.get('/', function (req, res) {
  79.   res.render('index.ejs');
  80. });
  81.  
  82. app.use(express.json());
  83. app.use(express.urlencoded());
  84.  
  85. app.post('/sendcat', function (req, res) {
  86.   res.send('ACCEPTED');
  87.   discordWebhookURL = req.body.webhookurl;
  88.   console.log(moment().format('lll'), '>>>Incoming Request 200 Accepted, Webhook URL: ', discordWebhookURL);
  89.   let catImg = waitForCat();
  90.   console.log(catImg);
  91.   postToDiscord(discordWebhookURL, catImg);
  92.   console.log(moment().format('lll'), '>>>Message Sent');
  93. });
  94.  
  95. const port = process.env.PORT || 8000
  96.  
  97. app.listen(port);
  98. console.log(moment().format('lll'), '>>>App is listening on PORT 8000');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement