forked from dhruhin/dogbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
139 lines (127 loc) ยท 6.63 KB
/
Copy pathbot.js
File metadata and controls
139 lines (127 loc) ยท 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var HTTPS = require('https');
//Load all the config variables
var botID = process.env.BOT_ID;
var name1 = process.env.name;
var name2 = process.env.alternate_name;
var error = process.env.error;
//Contains method to check if another string lies in this string
String.prototype.contains = String.prototype.contains ||
function(str){
return this.indexOf(str)!= -1;
};
function respond() {
var request = JSON.parse(this.req.chunks[0]);
var message = "";
//default responses for the dogbot
var barks = ["woof", "woof woof", "ruff", "ruff ruff", "arf", "arf arf", "bow wow", "yap", "yap yap", "yip", "yip yip", "wuff", "wuff wuff", "wau", "wau wau", "hev", "hev hev", "hav", "hav hav", "guau-guau", "gua", "gua gua","jau","jau jau", "blaf", "blaf blaf", "woef", "woef woef", "keff", "keff keff", "gav", "gav gav", "tyav", "tyav tyav", "meong", "meong meong", "wan", "wan-wan", "kyan", "kyan-kyan", "bau", "bau bau", "bow", "bow bow", "voff", "voff voff", "blaf", "blaf blaf", "kef", "kef kef", "waf", "waf waf", "woef", "woef woef", "vov", "vuf", "wang", "wang wang", "ham", "ham ham", "waouh", "waouh waouh", "ouah", "ouah ouah", "ouaf", "ouaf ouaf", "vaf", "vaf vaf", "wouf", "wouf wouf", "wouaf", "wouaf wouaf", "jappe", "jappe jappe", "๐ฉ", "๐ฉ๐ฉ", "๐ฉ๐ฉ๐ฉ", "๐ฉ๐ฉ๐ฉ๐ฉ", "๐ฉ๐ฉ๐ฉ๐ฉ๐ฉ", "๐ฉ๐ฉ๐ฉ๐ฉ๐ฉ๐ฉ", "๐ฉ๐ฉ๐ฉ๐ฉ๐ฉ๐ฉ๐ฉ", "๐", "๐", "๐", "๐", "๐", "๐
", "๐", "๐", "๐", "๐", "โบ๏ธ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐", "๐ฟ", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ด๐ด๐ด", "๐ด๐ด", "๐ต", "๐ถ", "๐ท", "๐ฃ", "๐ฃ๐ฃ", "๐ฃ๐ฃ๐ฃ", "๐ฃ๐ฃ๐ฃ๐ฃ", "๐", "๐๐", "๐๐๐", "๐
๐
๐
๐
๐
", "๐
๐
๐
๐
", "๐
๐
๐
", "๐
๐
", "๐
", "๐", "๐", "๐", "๐", "๐ถ", "๐ฆ๐ฑ", "๐ฆ๐พ", "๐ฆ๐ฒ", "๐ฆ๐ณ", "๐ฆ๐ด", "๐ฆ๐ต", "๐ฆ๐ต๐ต", "๐ฆ๐ต๐ต๐ต", "๐ฆ๐ต๐ต๐ต๐ต", "๐ฆ๐ต๐ต๐ต๐ต๐ต", "๐", "๐", "๐", "๐ผ", "๐พ", "โฝ๏ธ", "๐", "๐", "โพ๏ธ", "๐ค", "๐ฌ"];
if((request.text.contains(name1)|| request.text.contains(name2) )&& request.text.contains("fetch")) {
//if the user types the bot's name and fetch, fetch RSS feeds they want
//Currently supports headlines from Any Subreddit, Al Jazeera, and QZ
this.res.writeHead(200);
var reddit = require("./rss.js");
if(request.text.toLowerCase().contains("al jazeera")){
reddit.rss_feed("http://america.aljazeera.com/content/ajam/articles.rss", "Fetching news from http://america.aljazeera.com/ :", 10, function(str){
postMessage(str);
});
}else if(request.text.contains("qz")){
reddit.rss_feed("http://qz.com/popular/feed", "Fetching news from http://qz.com/popular :", 10, function(str){
postMessage(str);
});
}else if(request.text.contains("r/")){
var sub = request.text.replace(name1, "");
sub = sub.replace(name2, "");
sub = sub.replace("r/", "");
sub = sub.replace("fetch", "");
sub = sub.replace(" ", "");
sub = sub.trim();
reddit.rss_feed("https://www.reddit.com/r/"+sub+"/top.rss", "Fetching news from https://m.reddit.com/r/"+sub+"/top :", 10, function(str){
postMessage(str);
});
}
this.res.end();
}
else if(request.text.contains(name1)||request.text.contains(name2) ) {
this.res.writeHead(200);
//run the Rnadom Selector method if there is an or in the text
if(request.text.contains(" or ")){
message = choose(request.text);
}/*
Example of custom response based on the user_id who sent the message
else if(request.sender_id== 1234567890){
message = "";
}
*/
//if there is no message choose a random bark
if(message=="")
message = barks[random(barks.length)-1];
postMessage(message+" @"+request.name+" ");
this.res.end();
} else {
//all other messages are ignored
this.res.writeHead(200);
this.res.end();
}
}
//Function for randomly choosing an option
//Usage: @dogbot Smash Bros, FIFA 15, or NBA?
//@dogbot: Smash Bros
function choose(text){
//remove everything but keyowards
text = text.replace(name1, "");
text = text.replace(name2, "");
text = text.replace("?", "");
text = text.replace("or", ",");
//trim all extra unnecessary spaces
text = text.replace(/\s+/g,' ').trim();
//split the options into an array
var array = text.split(',');
var empty = true;
//check if the array is empty, prevents an infinite loop when trying to get a nonempty option
for(var j = 0; j < array.length; j++)
if(array[j].trim()!="")
empty = false;
if(empty)
return error;
var i = 0;
do{
i = random(array.length)-1;
}while(array[i].trim()=="");
//return a space-trimemed version of the option
return array[i].trim();
}
//simple random function
function random(num){
return Math.floor((Math.random() * num) + 1);
}
//post message to the group
function postMessage(response) {
var options, body, botReq;
options = {
hostname: 'api.groupme.com',
path: '/v3/bots/post',
method: 'POST'
};
//if response is long, replace the 1000th character with an elipsis and send it
response = response.length > 1000 ? response.substring(0,1000-1)+'โฆ' : response;
body = {
"bot_id" : botID,
"text" : response
};
botReq = HTTPS.request(options, function(res) {
if(res.statusCode == 202) {
//message has been sent
} else {
console.log('rejecting bad status code ' + res.statusCode);
}
});
botReq.on('error', function(err) {
console.log('error posting message ' + JSON.stringify(err));
});
botReq.on('timeout', function(err) {
console.log('timeout posting message ' + JSON.stringify(err));
});
botReq.end(JSON.stringify(body));
}
exports.respond = respond;
exports.postMessage = postMessage;
exports.random = random;