Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const http = require('http')
- const Bot = require('messenger-bot')
- const request=require("request-promise");
- const auth=require("./auth.json");
- const bad=require("./bad.json");
- const moment=require("moment-timezone");
- moment.locale("pl");
- moment.tz.setDefault("Europe/Warsaw");
- let bot = new Bot(auth);
- let stops={};
- let stopsInTrips={};
- let stopStartName="Obrońców Westerplatte";
- let stopEndName="Jaśkowa Dolina";
- const findConnection=(startId, endId)=>{
- request({url: "http://ckan2.multimediagdansk.pl/delays?stopId="+startId}).then((response)=>{
- let delays=JSON.parse(response).delay;
- let endStops=stopsInTrips.filter((stop)=>(stop.stopId == endId)); //znajdź przystanek docelowy
- for (let delay of delays){
- for (let stop of endStops) {
- if (stop.stopId != endId) {
- console.log("skipped");
- continue;
- }
- if (delay.tripId == stop.tripId && delay.routeId == stop.routeId) {
- let timeNow=moment().hour()*60+moment().minute();
- let timeEstimated=delay.estimatedTime.split(":")[0]*60+delay.estimatedTime.split(":")[1]*1;
- let timeDifference=timeEstimated-timeNow;
- console.log(delay.routeId, delay.headsign, timeDifference);
- }
- }
- }
- }).catch((error)=>{console.error(error);});
- };
- request({url: "https://ckan.multimediagdansk.pl/dataset/c24aa637-3619-4dc2-a171-a23eec8f2172/resource/3115d29d-b763-4af5-93f6-763b835967d6/download/stopsintrips.json"}).then((response)=>{
- console.log("Got stopsintrips.json");
- let today=new Date();
- let todayText=String(today.getFullYear()).padStart(2, "0")+"-"+String(today.getMonth()+1).padStart(2, "0")+"-"+String(today.getDate()).padStart(2, "0");
- stopsInTrips=JSON.parse(response)[todayText].stopsInTrip;
- request({url: "https://ckan.multimediagdansk.pl/dataset/c24aa637-3619-4dc2-a171-a23eec8f2172/resource/4c4025f0-01bf-41f7-a39f-d156d201b82b/download/stops.json"}).then((response)=>{
- console.log("Got stops.json");
- let today=new Date();
- let todayText=String(today.getFullYear()).padStart(2, "0")+"-"+String(today.getMonth()+1).padStart(2, "0")+"-"+String(today.getDate()).padStart(2, "0");
- stops=JSON.parse(response)[todayText].stops;
- let stopsStart=stops.filter((stop)=>(stop.stopName == stopStartName));
- let stopsEnd=stops.filter((stop)=>(stop.stopName == stopEndName));
- for (let stopStart of stopsStart) {
- for (let stopEnd of stopsEnd) {
- findConnection(stopStart.stopId, stopEnd.stopId);
- }
- }
- }).catch((error)=>{console.error(error);});
- }).catch((error)=>{console.error(error);});
- bot.on("message", (payload, reply)=>{
- let result="";
- let info="\n\n\nFor more information write 'help'";
- if(payload.message && payload.message.text){
- let textSplitted=payload.message.text.toLowerCase().split(" ") //text wpisywany do bota
- let badFound=false;
- for(var i=0; i<bad.length; i++){
- if(payload.message.text.toLowerCase().includes(bad[i])) {
- result="Watch your words! Be polite!" + info;
- badFound=true;
- }
- }
- if(badFound==false) {
- switch(textSplitted[0]) {
- case "sin":
- if (textSplitted.length == 1) result="Error: No argument." + info;
- else if (textSplitted.length == 2) {
- if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
- 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ę
- }
- else result="Error: Too many arguments." + info;
- break;
- case "cos":
- if (textSplitted.length == 1) result="Error: No argument." + info;
- else if (textSplitted.length == 2) {
- if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
- 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ę
- }
- else result="Error: Too many arguments." + info;
- break;
- case "tg":
- var x = (((textSplitted[1]*1)*Math.PI/180)-Math.PI/2)/(Math.PI);
- if (textSplitted.length == 1) result="Error: No argument." + info;
- else if (textSplitted.length == 2) {
- if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
- else if(Math.abs(Math.round(x)-x) < 0.001) result=payload.message.text + "° " + " *doesn't exist*" + info;
- 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ę
- }
- else result="Error: Too many arguments." + info;
- break;
- case "tan":
- var x = (((textSplitted[1]*1)*Math.PI/180)-Math.PI/2)/(Math.PI);
- if (textSplitted.length == 1) result="Error: No argument." + info;
- else if (textSplitted.length == 2) {
- if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
- else if(Math.abs(Math.round(x)-x) < 0.001) result=payload.message.text + "° " + " *doesn't exist*" + info;
- 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ę
- }
- else result="Error: Too many arguments." + info;
- break;
- case "ctg":
- var x = (((textSplitted[1]*1)*Math.PI/180)/(Math.PI));
- if (textSplitted.length == 1) result="Error: No argument." + info;
- else if (textSplitted.length == 2) {
- if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
- else if(Math.abs(Math.round(x)-x) < 0.001) result=payload.message.text + "° " + " it is an *indefinite value*" + info;
- 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ę
- }
- case "cot":
- var x = (((textSplitted[1]*1)*Math.PI/180)/(Math.PI));
- if (textSplitted.length == 1) result="Error: No argument." + info;
- else if (textSplitted.length == 2) {
- if (isNaN(textSplitted[1])) result="Error: Argument is not a number." + info; //isNan sprawdza czy string nie jest liczbą (is-Not-a-number)
- else if(Math.abs(Math.round(x)-x) < 0.001) result=payload.message.text + "° " + " it is an *indefinite value*" + info;
- 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ę
- }
- else result="Error: Too many arguments." + info;
- break;
- case "help":
- 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' ";
- break;
- default:
- result="Error: Unknown command." + info;
- }
- }
- }
- if(!result){
- result = "Error: Make sure the input contains text.";
- }
- reply({ text: result }, (error)=>{
- if (error) console.error("Error: Wystąpił błąd.");
- else console.error("Log: Wiadomość pomyślnie wysłana.");
- });
- });
- http.createServer(bot.middleware()).listen(process.env.PORT || 3000)
- console.log('Echo bot server running at port 3000.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement