Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c5c589fc authored by Qwexta's avatar Qwexta
Browse files

added: refactored olivia-console for testing purpose

parent fb2eec8b
Loading
Loading
Loading
Loading

LICENSE

0 → 100644
+21 −0
Original line number Diff line number Diff line
MIT License

Copyright (c) 2020-Present Hugo Lageneste

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+77 −1
Original line number Diff line number Diff line
# E
 No newline at end of file
<h1 align="center">
  <br>
  <img src="https://olivia-ai.org/img/icons/olivia-with-text.png" alt="Olivia's character" width="300">
  <br>
</h1>

<h4 align="center">📟Console client for Olivia</h4>

<p align="center">
  <a href="https://goreportcard.com/report/github.com/olivia-ai/olivia-console"><img src="https://goreportcard.com/badge/github.com/olivia-ai/olivia-console"></a>
  <img src="https://github.com/olivia-ai/olivia-console/workflows/Format%20checker/badge.svg">
</p>

<p align="center">
  <a href="https://olivia-ai.org">Website</a>
  <a href="https://discord.gg/wXDwTdy">Discord</a>
  <a href="#getting-started">Getting started</a>
  <a href="#contributors">Contributors</a>
  <a href="#license">License</a>
</p>

## Getting started
Console client for [Olivia](https://github.com/olivia-ai/olivia)

<p align="center">
<br>
<img src="./olivia-cc.svg">
<br>
</p>

### How to use it.
For the first time - you can simple run - ./main
Download it from [here](https://github.com/olivia-ai/olivia-console/releases)
It's will create new default config file, and new logfile.

#### Example of config file:
```json
{
 "port": "8080",
 "host": "localhost",
 "debug_level": "error",
 "bot_name": "Olivia",
 "user_token": "52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64981855ad8681d0d86d1e91e00167939cb6694"
}
```

#### Description:
* `bot_name` - name for your bot (default - Olivia)
* `debug_level` - verbosity (default - error)
* `host` - host where is running server part of olivia (default - localhost)
* `port` - the same about port
* `user_token` - your own token (default - generated on the first run)


## Contributors
<p align="center">
  <img alt="docker installation" height="85" src="https://i.imgur.com/6xr2zdp.png">
</p>

Thanks to @NerdDoc for the creation of this tool.

## Licence
<p align="center">
  <img src="https://i.imgur.com/9Xxtchv.png" height="90">
</p>

Licensed under MIT

<p align="center">
  <img width="60" src="https://olivia-ai.org/img/icons/olivia.png">
<p>

<p align="center">
  Made with ❤️ by <a href="https://github.com/hugolgst">Hugo Lageneste</a>
</p>

![Olivia's wave](https://olivia-ai.org/img/background-olivia.png)

chat/chat.go

0 → 100644
+163 −0
Original line number Diff line number Diff line
package chat

import (
	"crypto/rand"
	"encoding/json"
	"fmt"

	"github.com/gorilla/websocket"
)

// A Client will connect to the Olivia's server using the Token the Information and the Locale
type Client struct {
	Information *map[string]interface{}
	Locale      string
	Token       string
	Connection  *websocket.Conn
	Channel     chan string
}

// RequestMessage is the structure that uses entry connections to chat with the websocket
type RequestMessage struct {
	Type        int                    `json:"type"` // 0 for handshakes and 1 for messages
	Content     string                 `json:"content"`
	Token       string                 `json:"user_token"`
	Information map[string]interface{} `json:"information"`
	Locale      string                 `json:"locale"`
}

// ResponseMessage is the structure used to reply to the user through the websocket
type ResponseMessage struct {
	Content     string                 `json:"content"`
	Tag         string                 `json:"tag"`
	Information map[string]interface{} `json:"information"`
	Actions     []string               `json:"actions"`
}

// NewClient creates a new Client by generating a random token, and setting english as the
// default langauge.
// You need to give a pointer of the information map of your client.
// The host is also required with a boolean, if the SSL certificate is required.
func NewClient(host string, ssl bool, information *map[string]interface{}) (client Client, err error) {
	// Initialite the scheme and the url
	scheme := "ws"
	if ssl {
		scheme += "s"
	}

	url := fmt.Sprintf("%s://%s/websocket", scheme, host)
	fmt.Println(url)

	// Create the websocket connection
	connection, _, err := websocket.DefaultDialer.Dial(url, nil)
	if err != nil {
		return
	}

	client = Client{
		Information: information,
		Locale:      "en",
		Token:       generateToken(),
		Connection:  connection,
		Channel:     make(chan string),
	}

	// Execute the handshake
	err = client.handshake()

	return
}

// Close closes the websocket connection
func (client *Client) Close() {
	client.Connection.Close()
}

// SendMessage sends the given content to the websocket of Olivia and returns her response with
// an error.
func (client *Client) SendMessage(content string) (ResponseMessage, error) {
	// Marshal the RequestMessage with type 1
	message := RequestMessage{
		Type:        1,
		Content:     content,
		Token:       client.Token,
		Information: *client.Information,
		Locale:      client.Locale,
	}

	namefn := func(s string) []byte {
		b, _ := json.Marshal(s)
		return b
	}

	if btest, err2 := json.Marshal(message.Type); err2 == nil {
		fmt.Println("Type :", namefn("type"), btest)
	}
	if btest, err2 := json.Marshal(message.Content); err2 == nil {
		fmt.Println("Content :", namefn("content"), btest)
	}
	if btest, err2 := json.Marshal(message.Token); err2 == nil {
		fmt.Println("Token :", namefn("user_token"), btest)
	}
	if btest, err2 := json.Marshal(message.Information); err2 == nil {
		fmt.Println("Information :", namefn("information"), btest)
	}
	if btest, err2 := json.Marshal(message.Locale); err2 == nil {
		fmt.Println("Locale :", namefn("locale"), btest)
	}
	bytes, err := json.Marshal(message)
	if err != nil {
		return ResponseMessage{}, err
	}
	fmt.Println("bytes :", bytes)

	// Write the message to the websocket
	if err = client.Connection.WriteMessage(websocket.TextMessage, bytes); err != nil {
		return ResponseMessage{}, err
	}

	_, bytes, err = client.Connection.ReadMessage()
	if err != nil {
		return ResponseMessage{}, err
	}

	// Unmarshal the json content of the message
	var response ResponseMessage
	var res2 map[string]interface{}
	json.Unmarshal(bytes, &res2)
	fmt.Println("res :", res2)
	if err = json.Unmarshal(bytes, &response); err != nil {
		return ResponseMessage{}, err
	}

	return response, nil
}

// handshake performs the handshake request to the websocket
func (client *Client) handshake() error {
	// Marshal the RequestMessage with type 0
	bytes, err := json.Marshal(RequestMessage{
		Type:        0,
		Content:     "",
		Token:       client.Token,
		Information: *client.Information,
	})
	if err != nil {
		return err
	}

	// Write the message to the websocket
	if err = client.Connection.WriteMessage(websocket.TextMessage, bytes); err != nil {
		return err
	}

	return nil
}

// generateToken returns a random token of 50 characters
func generateToken() string {
	b := make([]byte, 50)
	rand.Read(b)

	return fmt.Sprintf("%x", b)
}

config.json

0 → 100644
+8 −0
Original line number Diff line number Diff line
{
 "port": "8080",
 "host": "localhost",
 "ssl": false,
 "debug_level": "error",
 "bot_name": "Olivia",
 "user_token": "3ce3c4bced5c13bdbfa3694d5b98cefe339cee2c8d66283ba7fbab8bd182"
}
 No newline at end of file

files/config.go

0 → 100644
+64 −0
Original line number Diff line number Diff line
package files

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"math/rand"
	"os"

	log "github.com/sirupsen/logrus"
)

// Configuration is the data required to start the tool
type Configuration struct {
	Port       string `json:"port"`
	Host       string `json:"host"`
	SSL        bool   `json:"ssl"`
	DebugLevel string `json:"debug_level"`
	BotName    string `json:"bot_name"`
	UserToken  string `json:"user_token"`
}

// FileExists checks if a file exists at a given path, it returns the condition
func FileExists(path string) bool {
	_, err := os.Stat(path)

	return err == nil
}

// GenerateToken returns a random token of 50 characters
func GenerateToken() string {
	b := make([]byte, 50)
	rand.Read(b)

	return fmt.Sprintf("%x", b)
}

// SetupConfig initializes the config file if it does not exists and returns the config itself
func SetupConfig(fileName string) *Configuration {
	config := Configuration{
		Port:       "8080",
		SSL:        false,
		Host:       "localhost",
		DebugLevel: "error",
		BotName:    "Olivia",
		UserToken:  GenerateToken(),
	}

	if FileExists(fileName) {
		// Read and parse the json file
		file, _ := ioutil.ReadFile(fileName)
		err := json.Unmarshal(file, &config)
		if err != nil {
			log.Fatal(err)
		}

		return &config
	}

	file, _ := json.MarshalIndent(config, "", " ")
	_ = ioutil.WriteFile(fileName, file, 0644)

	return &config
}
Loading