Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const http = require('http')
  2. const Bot = require('messenger-bot')
  3. const request=require("request-promise");
  4. const auth=require("./auth.json");
  5. const bad=require("./bad.json");
  6. const moment=require("moment-timezone");
  7. moment.locale("pl");
  8. moment.tz.setDefault("Europe/Warsaw");
  9. let bot = new Bot(auth);
  10.  
  11. let stops={};
  12. let stopsInTrips={};
  13. let stopStartName="Obrońców Westerplatte";
  14. let stopEndName="Jaśkowa Dolina";
  15.  
  16. const findConnection=(startId, endId)=>{
  17.     request({url: "http://ckan2.multimediagdansk.pl/delays?stopId="+startId}).then((response)=>{
  18.         let delays=JSON.parse(response).delay;
  19.         let endStops=stopsInTrips.filter((stop)=>(stop.stopId == endId)); //znajdź przystanek docelowy
  20.         for (let delay of delays){
  21.             for (let stop of endStops) {
  22.                 if (stop.stopId != endId) {
  23.                     console.log("skipped");
  24.                     continue;
  25.                 }
  26.                 if (delay.tripId == stop.tripId && delay.routeId == stop.routeId) {
  27.                     let timeNow=moment().hour()*60+moment().minute();
  28.                     let timeEstimated=delay.estimatedTime.split(":")[0]*60+delay.estimatedTime.split(":")[1]*1;
  29.                     let timeDifference=timeEstimated-timeNow;
  30.                     console.log(delay.routeId, delay.headsign, timeDifference);
  31.                 }
  32.             }
  33.         }
  34.     }).catch((error)=>{console.error(error);});
  35. };
  36.  
  37. request({url: "https://ckan.multimediagdansk.pl/dataset/c24aa637-3619-4dc2-a171-a23eec8f2172/resource/3115d29d-b763-4af5-93f6-763b835967d6/download/stopsintrips.json"}).then((response)=>{
  38.     console.log("Got stopsintrips.json");
  39.     let today=new Date();
  40.     let todayText=String(today.getFullYear()).padStart(2, "0")+"-"+String(today.getMonth()+1).padStart(2, "0")+"-"+String(today.getDate()).padStart(2, "0");
  41.     stopsInTrips=JSON.parse(response)[todayText].stopsInTrip;
  42.     request({url: "https://ckan.multimediagdansk.pl/dataset/c24aa637-3619-4dc2-a171-a23eec8f2172/resource/4c4025f0-01bf-41f7-a39f-d156d201b82b/download/stops.json"}).then((response)=>{
  43.         console.log("Got stops.json");
  44.         let today=new Date();
  45.         let todayText=String(today.getFullYear()).padStart(2, "0")+"-"+String(today.getMonth()+1).padStart(2, "0")+"-"+String(today.getDate()).padStart(2, "0");
  46.         stops=JSON.parse(response)[todayText].stops;
  47.         let stopsStart=stops.filter((stop)=>(stop.stopName == stopStartName));
  48.         let stopsEnd=stops.filter((stop)=>(stop.stopName == stopEndName));
  49.         for (let stopStart of stopsStart) {
  50.             for (let stopEnd of stopsEnd) {
  51.                 findConnection(stopStart.stopId, stopEnd.stopId);
  52.             }
  53.         }
  54.     }).catch((error)=>{console.error(error);});
  55. }).catch((error)=>{console.error(error);});
  56.  
  57. bot.on("message", (payload, reply)=>{
  58.     let result="";
  59.     let info="\n\n\nFor more information write 'help'";
  60.     if(payload.message && payload.message.text){
  61.         let textSplitted=payload.message.text.toLowerCase().split(" ") //text wpisywany do bota
  62.         let badFound=false;
  63.         for(var i=0; i<bad.length; i++){
  64.             if(payload.message.text.toLowerCase().includes(bad[i])) {
  65.                 result="Watch your words! Be polite!" + info;
  66.                 badFound=true;
  67.             }
  68.         }
  69.         if(badFound==false) {
  70.             switch(textSplitted[0]) {
  71.                 case "sin":
  72.                     if (textSplitted.length == 1) result="Error: No argument." + info;
  73.                     else if (textSplitted.length == 2) {
  74.                         if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
  75.                         else result=payload.message.text + "° equals: " + " *" + Math.round(Math.sin((textSplitted[1]*1)*Math.PI/180)*100)/100 + "*" + info; //string*1 automatycznie zamienia stringa na liczbę
  76.                     }
  77.                     else result="Error: Too many arguments." + info;
  78.                     break;
  79.                 case "cos":
  80.                     if (textSplitted.length == 1) result="Error: No argument." + info;
  81.                     else if (textSplitted.length == 2) {
  82.                         if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
  83.                         else result=payload.message.text + "° equals: " + " *" + Math.round(Math.cos((textSplitted[1]*1)*Math.PI/180)*100)/100 + "*" + info; //string*1 automatycznie zamienia stringa na liczbę
  84.                     }
  85.                     else result="Error: Too many arguments." + info;
  86.                     break;
  87.                 case "tg":
  88.                     var x = (((textSplitted[1]*1)*Math.PI/180)-Math.PI/2)/(Math.PI);
  89.                     if (textSplitted.length == 1) result="Error: No argument." + info;
  90.                     else if (textSplitted.length == 2) {
  91.                         if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
  92.                         else if(Math.abs(Math.round(x)-x) < 0.001) result=payload.message.text + "° " + " *doesn't exist*" + info;
  93.                         else result=payload.message.text + "° equals: " + " *" + Math.round(Math.tan((textSplitted[1]*1)*Math.PI/180)*100)/100 + "*" + info; //string*1 automatycznie zamienia stringa na liczbę
  94.                     }
  95.                     else result="Error: Too many arguments." + info;
  96.                     break;
  97.                 case "tan":
  98.                     var x = (((textSplitted[1]*1)*Math.PI/180)-Math.PI/2)/(Math.PI);
  99.                     if (textSplitted.length == 1) result="Error: No argument." + info;
  100.                     else if (textSplitted.length == 2) {
  101.                         if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
  102.                         else if(Math.abs(Math.round(x)-x) < 0.001) result=payload.message.text + "° " + " *doesn't exist*" + info;
  103.                         else result=payload.message.text + "° equals: " + " *" + Math.round(Math.tan((textSplitted[1]*1)*Math.PI/180)*100)/100 + "*" + info; //string*1 automatycznie zamienia stringa na liczbę
  104.                     }
  105.                     else result="Error: Too many arguments." + info;
  106.                     break;
  107.                 case "ctg":
  108.                     var x = (((textSplitted[1]*1)*Math.PI/180)/(Math.PI));
  109.                     if (textSplitted.length == 1) result="Error: No argument." + info;
  110.                     else if (textSplitted.length == 2) {
  111.                         if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
  112.                         else if(Math.abs(Math.round(x)-x) < 0.001) result=payload.message.text + "° " + " it is an *indefinite value*" + info;
  113.                         else result=payload.message.text + "° equals: " + " *" + Math.round(Math.pow(Math.tan((textSplitted[1]*1)*Math.PI/180),-1)*100)/100 + "*" + info;  //string*1 automatycznie zamienia stringa na liczbę
  114.                     }
  115.                 case "cot":
  116.                     var x = (((textSplitted[1]*1)*Math.PI/180)/(Math.PI));
  117.                     if (textSplitted.length == 1) result="Error: No argument." + info;
  118.                     else if (textSplitted.length == 2) {
  119.                         if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
  120.                         else if(Math.abs(Math.round(x)-x) < 0.001) result=payload.message.text + "° " + " it is an *indefinite value*" + info;
  121.                         else result=payload.message.text + "° equals: " + " *" + Math.round(Math.pow(Math.tan((textSplitted[1]*1)*Math.PI/180),-1)*100)/100 + "*" + info;  //string*1 automatycznie zamienia stringa na liczbę
  122.                     }
  123.                     else result="Error: Too many arguments." + info;
  124.                     break;
  125.                 case "help":
  126.                     result = "Welcome! \n\nThis bot is used to calculate the value of trigonometric functions\n\nTo use it, enter the function (sin, cos, tg/tan, ctg), followed by an angle in degrees \n\n\nFor example: \n'sin 30' ";
  127.                     break;
  128.                 default:
  129.                     result="Error: Unknown command." + info;
  130.                 }
  131.                 }
  132.             }  
  133.        
  134.         if(!result){
  135.             result = "Error: Make sure the input contains text.";
  136.         }
  137.  
  138.         reply({ text: result }, (error)=>{
  139.             if (error) console.error("Error: Wystąpił błąd.");
  140.             else console.error("Log: Wiadomość pomyślnie wysłana.");
  141.         });
  142.     });
  143.  
  144. http.createServer(bot.middleware()).listen(process.env.PORT || 3000)
  145. console.log('Echo bot server running at port 3000.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement