server.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Author: Matthew Shiel
  2. package main
  3. import (
  4. "embed"
  5. "encoding/json"
  6. "fmt"
  7. "html/template"
  8. "io/ioutil"
  9. "log"
  10. "net/http"
  11. "strings"
  12. "git.riomhaire.com/gremlin/elizaservice/eliza"
  13. "github.com/jaffee/commandeer"
  14. "github.com/labstack/echo/v4"
  15. "github.com/labstack/echo/v4/middleware"
  16. )
  17. //go:embed web/*
  18. var web embed.FS
  19. //go:embed templates/*
  20. var templates embed.FS
  21. //go:embed bots/*
  22. var bots embed.FS
  23. type Main struct {
  24. BotName string `help:"What This Bot is Called."`
  25. Port int `help:"What port bot is listening too"`
  26. }
  27. func NewMain() *Main {
  28. return &Main{
  29. BotName: "Eliza",
  30. Port: 8070,
  31. }
  32. }
  33. func findPersonality(name string) (personality eliza.Personality, err error) {
  34. filename := "bots/" + name + ".json"
  35. jsonFile, err := bots.Open(filename)
  36. // if we os.Open returns an error then handle it
  37. if err != nil {
  38. return
  39. }
  40. // fmt.Println("Successfully Opened ", filename)
  41. byteValue, _ := ioutil.ReadAll(jsonFile)
  42. // we initialize our Users array
  43. personality = eliza.Personality{}
  44. // we unmarshal our byteArray which contains our
  45. // jsonFile's content into 'users' which we defined above
  46. json.Unmarshal(byteValue, &personality)
  47. // defer the closing of our jsonFile so that we can parse it later on
  48. defer jsonFile.Close()
  49. return
  50. }
  51. func chantboInteractiontEndpoint(c echo.Context) error {
  52. // Extract question from GET request
  53. question := c.QueryParam("value")
  54. botname := c.QueryParam("bot")
  55. // default to eliza
  56. if len(botname) == 0 {
  57. botname = "eliza"
  58. }
  59. personality, err := findPersonality(strings.ToLower(botname)) // files are in lowercase
  60. if err != nil {
  61. fmt.Fprintf(c.Response(), "%s", err.Error())
  62. return nil
  63. }
  64. // Return Eliza's response's so long as the user doesn't give a quit statement
  65. character := eliza.NewBotPersonality(&personality)
  66. answer := character.ReplyTo(question)
  67. // Return Eliza's answer
  68. fmt.Fprintf(c.Response(), "%s", answer)
  69. return nil
  70. }
  71. func chantbotEndpoint(c echo.Context) error {
  72. c.Response().WriteHeader(http.StatusOK)
  73. c.Response().Header().Add("Content-Type", "text/html")
  74. // Note the call to ParseFS instead of Parse
  75. personality := c.Param("personality")
  76. t, err := template.ParseFS(templates, fmt.Sprintf("templates/%s.html", personality))
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. // respond with the output of template execution
  81. t.Execute(c.Response(), struct {
  82. Id string
  83. GivenName string
  84. FamilyName string
  85. Name string
  86. Bot string
  87. }{
  88. Id: c.QueryParam("user_id"),
  89. GivenName: c.QueryParam("given_name"),
  90. FamilyName: c.QueryParam("family_name"),
  91. Name: c.QueryParam("name"),
  92. Bot: strings.Title(strings.ToLower(personality)),
  93. })
  94. return nil
  95. }
  96. func (m *Main) Run() error {
  97. fmt.Println(`
  98. ####### # ### ####### #
  99. # # # # # #
  100. # # # # # #
  101. ##### # # # # #
  102. # # # # #######
  103. # # # # # #
  104. ####### ####### ### ####### # #
  105. `)
  106. fmt.Println()
  107. e := echo.New()
  108. var contentHandler = echo.WrapHandler(http.FileServer(http.FS(web)))
  109. var contentRewrite = middleware.Rewrite(map[string]string{"/*": "/web/$1"})
  110. e.GET("/user-input", chantboInteractiontEndpoint)
  111. e.GET("/chatbot/:personality", chantbotEndpoint)
  112. e.GET("/*", contentHandler, contentRewrite)
  113. e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", m.Port)))
  114. return nil
  115. }
  116. func main() {
  117. err := commandeer.Run(NewMain())
  118. if err != nil {
  119. fmt.Println(err)
  120. }
  121. }