|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +class BotWeb { |
| 4 | + |
| 5 | + static init(config){ |
| 6 | + |
| 7 | + if (!BotWeb.instance) { |
| 8 | + var s = this; |
| 9 | + BotWeb.instance = new s(config); |
| 10 | + } |
| 11 | + return BotWeb.instance; |
| 12 | + |
| 13 | + } |
| 14 | + |
| 15 | + constructor(config) { |
| 16 | + this.Active = false; |
| 17 | + this.Database = {}; |
| 18 | + this.Database["BotConfig"] = {}; |
| 19 | + this.Database["BotConfig"]["input"] = config.input; |
| 20 | + this.Database["BotConfig"]["bot_name"] = config.name; |
| 21 | + this.Database["BotConfig"]["create_as"] = new Date(); |
| 22 | + this.events(); |
| 23 | + this.output(); |
| 24 | + this.online(); |
| 25 | + } |
| 26 | + |
| 27 | + events(){ |
| 28 | + |
| 29 | + this.Database["EventsTrigger"] = {}; |
| 30 | + this.Database["EventsTrigger"]["/search"] = function(msg){ |
| 31 | + msg = encodeURI(msg).trim(); |
| 32 | + var url = "https://pt.wikipedia.org/w/api.php?action=query&list=search&origin=*&srsearch="+msg+"&format=json"; |
| 33 | + $.get(url, function(res){ |
| 34 | + console.log(typeof res); |
| 35 | + if(!'query' in res){ |
| 36 | + throw "Erro na funcao Search"; |
| 37 | + } |
| 38 | + if(!'search' in res.query){ |
| 39 | + throw "Erro na funcao Search"; |
| 40 | + } |
| 41 | + var response = res.query.search[0]; |
| 42 | + $(".BotWeb-Response").html(`<strong>${response.title}</strong><br/><p>${response.snippet}</p>`); |
| 43 | + $(".BotWeb-Response").css("display", "flex"); |
| 44 | + $(".BotWeb-Modal").css("width", parseInt($(".BotWeb-Modal-Inputs").width()) + "px"); |
| 45 | + $(".BotWeb-Response").css("width", "100%"); |
| 46 | + $(".BotWeb-Response").addClass("animated bounceInDown"); |
| 47 | + setTimeout(() => { $(".BotWeb-Response").removeClass("animated bounceInDown"); }, 4000); |
| 48 | + //var d = JSON.parse(Res); |
| 49 | + //console.log(d); |
| 50 | + }); |
| 51 | + }; |
| 52 | + this.Database["EventsTrigger"]["/temperature"] = function(msg){ |
| 53 | + console.log("TEMPERATURE !!!"); |
| 54 | + }; |
| 55 | + this.Database["EventsTrigger"]["/andress"] = function(msg){ |
| 56 | + console.log("ANDRESS !!!"); |
| 57 | + }; |
| 58 | + this.Database["EventsTrigger"]["/youtube"] = function(msg){ |
| 59 | + console.log("SEARCH YOUTUBE !!!"); |
| 60 | + }; |
| 61 | + this.Database["EventsTrigger"]["/cotacao"] = function(msg){ |
| 62 | + console.log("COTACAO !!!"); |
| 63 | + }; |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + output(){ |
| 68 | + |
| 69 | + function NewObj(n, c){ |
| 70 | + var v = document.createElement(n); |
| 71 | + v.className = c; |
| 72 | + return v; |
| 73 | + } |
| 74 | + |
| 75 | + var input = this.Database["BotConfig"]["input"], |
| 76 | + view = input.parentNode, |
| 77 | + clone = input, |
| 78 | + modal = NewObj("div", "BotWeb-Modal"), |
| 79 | + modalInputs = NewObj("div", "BotWeb-Modal-Inputs"); |
| 80 | + |
| 81 | + var height = $(clone).height(); |
| 82 | + |
| 83 | + input.remove(); |
| 84 | + |
| 85 | + this.Database["Template"] = {}; |
| 86 | + this.Database["Template"]["BotWebBot"] = `<div class='BotWeb-Bot animated bounceIn' style='height:${height}px !important'>BotWeb</div>`; |
| 87 | + modalInputs.append(clone[0]); |
| 88 | + $(modal).html("<div class='BotWeb-Response'></div>"); |
| 89 | + modal.append(modalInputs); |
| 90 | + $("body").append(modal); |
| 91 | + this.Database["BotConfig"]["input"] = clone[0]; |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + OnMessage(msg){ |
| 96 | + |
| 97 | + var input = this.Database["BotConfig"]["input"], |
| 98 | + msgOrigin = msg, |
| 99 | + regexBot = /@botweb/ig, |
| 100 | + regexs = [ |
| 101 | + /\/search/ig, |
| 102 | + /\/temperature/ig, |
| 103 | + /\/andress/ig, |
| 104 | + /\/youtube/ig, |
| 105 | + /\/cotacao/ig |
| 106 | + ]; |
| 107 | + |
| 108 | + msg = msg.toLowerCase(); |
| 109 | + if(this.Active){ |
| 110 | + var t = regexs.filter((v) => { |
| 111 | + return v.test(msg) == true; |
| 112 | + }); |
| 113 | + if(t.length > 0){ |
| 114 | + var value = msg.replace(t[0], "").trim(), |
| 115 | + i = t[0].source.toString(); |
| 116 | + var n = i.replace("/\//", ""); |
| 117 | + n = "/" + n.substr(2); |
| 118 | + this.Database["EventsTrigger"][n](value); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if(regexBot.test(msg) && this.Active == false){ |
| 123 | + $(Bot.Database.Template.BotWebBot).insertBefore($(".BotWebInput")); |
| 124 | + $(".BotWeb-Modal").css("box-shadow","0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23)"); |
| 125 | + $(input).css("border-top-right-radius","3px"); |
| 126 | + $(input).css("border-bottom-right-radius","3px"); |
| 127 | + input.value = msg.replace(regexBot, ""); |
| 128 | + this.Active = true; |
| 129 | + this.OnVerifyBotter(input); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + OnVerifyBotter(i){ |
| 135 | + |
| 136 | + function Botter(){ |
| 137 | + |
| 138 | + if($(i).val().length == 0 && $(".BotWeb-Response").css("display") == "flex"){ |
| 139 | + |
| 140 | + $(".BotWeb-Response").html(""); |
| 141 | + $(".BotWeb-Response").css("display","none"); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + this.Database["OnBotter"] = setInterval(Botter, 4000); |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + online(){ |
| 152 | + |
| 153 | + var input = this.Database["BotConfig"]["input"], |
| 154 | + self = this; |
| 155 | + $(".BotWebInput").keyup(function(e){ |
| 156 | + self.OnMessage(input.value); |
| 157 | + }); |
| 158 | + //this.OnMessage(input.value) |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + toString() { |
| 163 | + |
| 164 | + return JSON.stringify(this); |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments