Skip to content
Permalink
Browse files

Adding cleverbot api capabilities

  • Loading branch information...
Depado committed Feb 24, 2016
1 parent f3dd7d4 commit f0dde58aab42bda050f9c5e3c816dbf053b2feed
@@ -21,7 +21,8 @@ type Configuration struct {
Admins []string `yaml:"admins"`
GoogleAPIKey string `yaml:"google_api_key"`
YandexTrnslKey string `yaml:"yandex_trnsl_key"`
YandexDictKey string `yaml:"yandex_dict_key"`
CleverBotUser string `yaml:"cleverbot_user"`
CleverBotKey string `yaml:"cleverbot_key"`
Lang string `yaml:"lang"`
UserCommandCharacter string `yaml:"user_command_character"`
}
16 main.go
@@ -2,15 +2,16 @@ package main

import (
"crypto/tls"
"flag"
"log"
"strings"

flag "github.com/ogier/pflag"
"github.com/thoj/go-ircevent"

"github.com/depado/go-b0tsec/configuration"
"github.com/depado/go-b0tsec/database"
"github.com/depado/go-b0tsec/plugins"
"github.com/depado/go-b0tsec/plugins/cleverbot"
_ "github.com/depado/go-b0tsec/pluginsinit"
)

@@ -20,21 +21,24 @@ func main() {
cnf := configuration.Config

// Argument parsing
configuration.ConfPath = flag.String("c", "conf.yml", "Local path to configuration file.")
configuration.ConfPath = flag.StringP("config", "c", "conf.yml", "Local path to configuration file.")
flag.Parse()

// Load the configuration of the bot
configuration.Load()

// Storage initialization
if err = cleverbot.Clever.Initialize(); err != nil {
log.Fatal(err)
}

// Storage initialization and defering Close
if err := database.BotStorage.Open(); err != nil {
log.Fatalf("something went wrong with the databse : %v", err)
}
defer database.BotStorage.Close()

// Plugins initialization and defering Stop
plugins.Start()

// Defer all Close/Stop methods
defer database.BotStorage.Close()
defer plugins.Stop()

// Bot initialization
@@ -0,0 +1,96 @@
package cleverbot

import (
"bytes"
"encoding/json"
"fmt"
"net/http"

"github.com/depado/go-b0tsec/configuration"
)

type CleverBot struct {
Nick string `json:"nick"`
User string `json:"user"`
Key string `json:"key"`
}

var Clever CleverBot

func (c *CleverBot) Initialize() error {
c.Nick = "gobot"
c.User = configuration.Config.CleverBotUser
c.Key = configuration.Config.CleverBotKey
if c.Key != "" && c.User != "" {
return c.Create()
}
return nil
}

func (c *CleverBot) Create() error {
var err error
var m []byte
if m, err = json.Marshal(c); err != nil {
return err
}
req, err := http.NewRequest("POST", "https://cleverbot.io/1.0/create", bytes.NewBuffer(m))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var rsp CleverBotCreationResponse
if err = json.NewDecoder(resp.Body).Decode(&rsp); err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("Could not create the bot : %s", rsp.Status)
}
return err
}

func (c *CleverBot) Query(text string) (string, error) {
var err error
var m []byte

r := CleverBotRequest{c.Nick, c.User, c.Key, text}

if m, err = json.Marshal(r); err != nil {
return "", err
}
req, err := http.NewRequest("POST", "https://cleverbot.io/1.0/ask", bytes.NewBuffer(m))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
var rsp CleverBotQueryResponse
if err = json.NewDecoder(resp.Body).Decode(&rsp); err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("Could not query the bot : %s", rsp.Status)
}
return rsp.Response, err
}

type CleverBotRequest struct {
Nick string `json:"nick"`
User string `json:"user"`
Key string `json:"key"`
Text string `json:"text"`
}

type CleverBotCreationResponse struct {
Status string `json:"status"`
Nick string `json:"nick"`
}

type CleverBotQueryResponse struct {
Status string `json:"status"`
Response string `json:"response"`
}
@@ -0,0 +1,74 @@
package cleverbot

import (
"log"
"strings"

"github.com/depado/go-b0tsec/configuration"
"github.com/depado/go-b0tsec/plugins"
"github.com/depado/go-b0tsec/utils"
"github.com/thoj/go-ircevent"
)

const (
command = "clever"
)

// Command is the cleverbot.Command type
type Command struct {
Started bool
}

func init() {
plugins.Commands[command] = new(Command)
}

// Help displays the help for the plugin
func (c *Command) Help(ib *irc.Connection, from string) {
if !c.Started {
return
}
ib.Privmsg(from, "This command asks something to the Cleverbot API.")
ib.Privmsg(from, "Example : !clever What time is it ?")
}

// Get actually acts
func (c *Command) Get(ib *irc.Connection, from string, to string, args []string) {
if !c.Started {
return
}
if to == configuration.Config.BotName {
to = from
}
if len(args) > 0 {
answer, err := Clever.Query(strings.Join(args, " "))
if err != nil {
log.Println(err)
return
}
if to == from {
ib.Privmsg(to, answer)
} else {
ib.Privmsgf(to, "%s: %s", from, answer)
}
}
}

// Start starts the plugin and returns any occurred error, nil otherwise
func (c *Command) Start() error {
if utils.StringInSlice(command, configuration.Config.Commands) {
c.Started = true
}
return nil
}

// Stop stops the plugin and returns any occurred error, nil otherwise
func (c *Command) Stop() error {
c.Started = false
return nil
}

// IsStarted returns the state of the plugin
func (c *Command) IsStarted() bool {
return c.Started
}
@@ -0,0 +1,72 @@
package cleverbot

import (
"log"
"strings"

"github.com/thoj/go-ircevent"

"github.com/depado/go-b0tsec/configuration"
"github.com/depado/go-b0tsec/plugins"
"github.com/depado/go-b0tsec/utils"
)

const (
middlewareName = "clever"
)

// Middleware is the actual markov.Middleware
type Middleware struct {
Started bool
}

func init() {
plugins.Middlewares[middlewareName] = new(Middleware)
}

// Get actually operates on the message
func (m *Middleware) Get(ib *irc.Connection, from string, to string, message string) {
if !m.Started {
return
}
cnf := configuration.Config
if to == cnf.BotName {
to = from
}
if from != cnf.BotName {
if !strings.HasPrefix(message, "!") && strings.Contains(message, cnf.BotName) {
sp := strings.Fields(message)
if len(sp) > 1 {
answer, err := Clever.Query(strings.Join(strings.Fields(message)[1:], " "))
if err != nil {
log.Println(err)
return
}
if to == from {
ib.Privmsg(to, answer)
} else {
ib.Privmsgf(to, "%s: %s", from, answer)
}
}
}
}
}

// Start starts the middleware and returns any occurred error, nil otherwise
func (m *Middleware) Start() error {
if utils.StringInSlice(middlewareName, configuration.Config.Middlewares) {
m.Started = true
}
return nil
}

// Stop stops the middleware and returns any occurred error, nil otherwise
func (m *Middleware) Stop() error {
m.Started = false
return nil
}

// IsStarted returns the state of the middleware
func (m *Middleware) IsStarted() bool {
return m.Started
}
@@ -5,6 +5,7 @@ import (
_ "github.com/depado/go-b0tsec/plugins/afk"
_ "github.com/depado/go-b0tsec/plugins/anon"
_ "github.com/depado/go-b0tsec/plugins/choice"
_ "github.com/depado/go-b0tsec/plugins/cleverbot"
_ "github.com/depado/go-b0tsec/plugins/core/config"
_ "github.com/depado/go-b0tsec/plugins/core/help"
_ "github.com/depado/go-b0tsec/plugins/dice"

0 comments on commit f0dde58

Please sign in to comment.
You can’t perform that action at this time.