-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from Harkishen-Singh/general-conversations
support for General conversations
- Loading branch information
Showing
10 changed files
with
387 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,272 @@ | ||
package messages | ||
|
||
import ( | ||
// "fmt" | ||
"fmt" | ||
"net/http" | ||
// "encoding/json" | ||
// "os" | ||
"encoding/json" | ||
"os" | ||
"io/ioutil" | ||
"math/rand" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type response struct { | ||
username string | ||
message string | ||
} | ||
|
||
type jsonResponse struct { | ||
Status bool `json:"status"` | ||
Message string `json:"message"` | ||
Show bool `json:"show"` | ||
Result []string `json:"result"` | ||
} | ||
|
||
// Messages json parser for default string types | ||
type Messages struct { | ||
InitialGreetingsName []string `json:"initial-greetings-name"` | ||
InitialGreetingsPlain []string `json:"initial-greetings-plain"` | ||
Help []string `json:"help"` | ||
About []string `json:"about"` | ||
Age []string `json:"age"` | ||
Birthday []string `json:"birthday"` | ||
} | ||
|
||
// Messagesreplies json parser for default reply string types | ||
type Messagesreplies struct { | ||
InitialGreetingsName []string `json:"initial-greetings-name"` | ||
InitialGreetingsPlain []string `json:"initial-greetings-plain"` | ||
Help []string `json:"help"` | ||
About []string `json:"about"` | ||
Age []string `json:"age"` | ||
Birthday []string `json:"birthday"` | ||
} | ||
|
||
var ( | ||
messagesParser Messages | ||
messagesRepliesParser Messagesreplies | ||
resp jsonResponse | ||
username, speak string | ||
) | ||
|
||
func loadJSONParsers() { | ||
func loadJSONParsers(name string) { | ||
|
||
fmt.Println("Loading JSON parsers....") | ||
messagesFile, err := os.Open("messages/messages.json") | ||
messagesRepliesFile, err2 := os.Open("messages/messages_replies.json") | ||
bytvalMF, _ := ioutil.ReadAll(messagesFile) | ||
bytvalMRF, _ := ioutil.ReadAll(messagesRepliesFile) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if err2 != nil { | ||
panic(err2) | ||
} | ||
username = name | ||
|
||
err1 := json.Unmarshal(bytvalMF, &messagesParser) | ||
err2 = json.Unmarshal(bytvalMRF, &messagesRepliesParser) | ||
if err1 != nil { | ||
panic(err1) | ||
} | ||
if err2 != nil { | ||
panic(err2) | ||
} | ||
|
||
} | ||
|
||
func filterForMessagesComparision(s string) (sr string) { | ||
|
||
sr = strings.Replace(s, "?", " ", -1) | ||
sr = strings.Replace(sr, "%", " ", -1) | ||
sr = strings.Replace(sr, "#", " ", -1) | ||
sr = strings.Replace(sr, "$", " ", -1) | ||
sr = strings.Replace(sr, "@", " ", -1) | ||
sr = strings.Replace(sr, "&", " ", -1) | ||
sr = strings.Replace(sr, "^", " ", -1) | ||
sr = strings.Replace(sr, "*", " ", -1) | ||
return | ||
} | ||
|
||
// GeneralConvHandler handles stuff related to general conversation | ||
func GeneralConvHandler(req response, res http.ResponseWriter) { | ||
func GeneralConvHandler(req, name string, res http.ResponseWriter) string { | ||
|
||
fmt.Println("General conversation...") | ||
rand.Seed(time.Now().UnixNano()) | ||
loadJSONParsers(name) | ||
message := filterForMessagesComparision(req) | ||
match := false | ||
|
||
// determine type of message | ||
if !match { | ||
isGreetingPlain := func(s string) bool { | ||
for i:=0; i< len(messagesParser.InitialGreetingsPlain); i++ { | ||
if strings.ToLower(s) == messagesParser.InitialGreetingsPlain[i] { | ||
match = true | ||
return true | ||
} | ||
} | ||
return false | ||
}(message) | ||
|
||
if isGreetingPlain { | ||
temp := greetingPlainController(message) | ||
resp = jsonResponse{true, temp, true, nil} | ||
speak = temp | ||
marshalled, _ := json.Marshal(resp) | ||
res.Write(marshalled) | ||
} | ||
} | ||
|
||
|
||
if !match { | ||
isGreetingName := func(s string) bool { | ||
for i:=0; i< len(messagesParser.InitialGreetingsName); i++ { | ||
if strings.ToLower(s) == messagesParser.InitialGreetingsName[i] { | ||
fmt.Println("contains ", strings.ToLower(s), " ", messagesParser.InitialGreetingsName[i]) | ||
match = true | ||
return true | ||
} | ||
} | ||
return false | ||
}(message) | ||
|
||
if isGreetingName { | ||
temp := greetingNameController(message) | ||
resp = jsonResponse{true, temp, true, nil} | ||
speak = temp | ||
marshalled, _ := json.Marshal(resp) | ||
res.Write(marshalled) | ||
} | ||
} | ||
|
||
if !match { | ||
isHelp := func(s string) bool { | ||
for i:=0; i< len(messagesParser.Help); i++ { | ||
if strings.ToLower(s) == messagesParser.Help[i] { | ||
match = true | ||
return true | ||
} | ||
} | ||
return false | ||
}(message) | ||
|
||
if isHelp { | ||
temp := helpController(message) | ||
resp = jsonResponse{true, temp, true, nil} | ||
speak = temp | ||
marshalled, _ := json.Marshal(resp) | ||
res.Write(marshalled) | ||
} | ||
} | ||
|
||
if !match { | ||
isAbout := func(s string) bool { | ||
for i:=0; i< len(messagesParser.About); i++ { | ||
if strings.ToLower(s) == messagesParser.About[i] { | ||
match = true | ||
return true | ||
} | ||
} | ||
return false | ||
}(message) | ||
|
||
if isAbout { | ||
temp := aboutController(message) | ||
resp = jsonResponse{true, temp, true, nil} | ||
speak = temp | ||
marshalled, _ := json.Marshal(resp) | ||
res.Write(marshalled) | ||
} | ||
} | ||
|
||
if !match { | ||
fmt.Println("inside") | ||
isAge := func(s string) bool { | ||
for i:=0; i< len(messagesParser.Age); i++ { | ||
if strings.ToLower(s) == messagesParser.Age[i] { | ||
match = true | ||
return true | ||
} | ||
} | ||
return false | ||
}(message) | ||
|
||
if isAge { | ||
temp := ageController(message) | ||
fmt.Println("temp age is ", temp) | ||
resp = jsonResponse{true, temp, true, nil} | ||
speak = temp | ||
marshalled, _ := json.Marshal(resp) | ||
res.Write(marshalled) | ||
} | ||
} | ||
|
||
if !match { | ||
isBirthday := func(s string) bool { | ||
for i:=0; i< len(messagesParser.Birthday); i++ { | ||
if strings.ToLower(s) == messagesParser.Birthday[i] { | ||
match = true | ||
return true | ||
} | ||
} | ||
return false | ||
}(message) | ||
|
||
if isBirthday { | ||
temp := birthdayController(message) | ||
resp = jsonResponse{true, temp, true, nil} | ||
speak = temp | ||
marshalled, _ := json.Marshal(resp) | ||
res.Write(marshalled) | ||
} | ||
} | ||
|
||
return speak | ||
|
||
} | ||
|
||
func helpController(s string) string { | ||
|
||
numb := rand.Intn(len(messagesRepliesParser.Help)) | ||
return messagesRepliesParser.Help[numb] | ||
|
||
} | ||
|
||
func greetingPlainController(s string) string { | ||
|
||
numb := rand.Intn(len(messagesRepliesParser.InitialGreetingsPlain)) | ||
return messagesRepliesParser.InitialGreetingsPlain[numb] | ||
|
||
} | ||
|
||
func greetingNameController(s string) string { | ||
|
||
numb := rand.Intn(len(messagesRepliesParser.InitialGreetingsName)) | ||
temp := messagesRepliesParser.InitialGreetingsName[numb] | ||
reply := fmt.Sprintf(temp, username) // note the formatter in messages_replies used | ||
return reply | ||
|
||
} | ||
|
||
func aboutController(s string) string { | ||
|
||
numb := rand.Intn(len(messagesRepliesParser.About)) | ||
return messagesRepliesParser.About[numb] | ||
|
||
} | ||
|
||
func ageController(s string) string { | ||
|
||
numb := rand.Intn(len(messagesRepliesParser.Age)) | ||
return messagesRepliesParser.Age[numb] | ||
|
||
} | ||
|
||
func birthdayController(s string) string { | ||
|
||
numb := rand.Intn(len(messagesRepliesParser.Birthday)) | ||
return messagesRepliesParser.Birthday[numb] | ||
|
||
} |
Oops, something went wrong.