Skip to content

Commit 8d78707

Browse files
authored
Add Word Dictionary (#424)
This parser provides definition for the first word. ->provides meaningful message when null, numeric or special character's are entered. ->provides message when there is no definition found for the given word. ->it only considers first word from the entered sentences and ignores the rest.
1 parent 385a5db commit 8d78707

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Parsers/Word Dictionary.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
activation_example:!dictionary type a word
3+
regex:!dictionary
4+
flags:gmi
5+
order:200
6+
stop_processing:false
7+
*/
8+
9+
var word = current.text.replace(/!dictionary/gmi, "").trim();
10+
if (word == '') {
11+
new x_snc_slackerbot.Slacker().send_chat(current, 'Please type a word', false);
12+
} else {
13+
var regex = /[0-9!@#$%^&*(),.?":{}|<>]/;
14+
if (regex.test(word)) {
15+
new x_snc_slackerbot.Slacker().send_chat(current, 'Please type a word with only characters', false);
16+
} else {
17+
var wordsArray = word.split(" ");
18+
var firstWord = wordsArray[0];
19+
20+
var dictionary = new sn_ws.RESTMessageV2();
21+
dictionary.setEndpoint('https://api.dictionaryapi.dev/api/v2/entries/en/' + firstWord);
22+
dictionary.setHttpMethod("GET");
23+
dictionary.setRequestHeader("Accept", "application/json");
24+
var response = dictionary.execute();
25+
var responseBody = response.getBody();
26+
var statuscode = response.getStatusCode();
27+
var responseJson = JSON.parse(responseBody);
28+
var sendsentence = "";
29+
if (statuscode == 200) {
30+
var meaning = responseJson[0].meanings[0].definitions[0].definition;
31+
sendsentence = firstWord + ': ' + meaning;
32+
} else {
33+
sendsentence = "Sorry pal, we couldn't find definitions for the word you were looking for.";
34+
}
35+
new x_snc_slackerbot.Slacker().send_chat(current, sendsentence, false);
36+
}
37+
}

0 commit comments

Comments
 (0)