Skip to content

Commit

Permalink
go-chat-bot#97 add protocol and server to bot config for each protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
bnfinet committed Dec 2, 2018
1 parent 07092db commit b8f4369
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 25 deletions.
21 changes: 17 additions & 4 deletions cmd_test.go
Expand Up @@ -72,7 +72,12 @@ func newBot() *Bot {
return New(&Handlers{
Response: responseHandler,
Errored: errorHandler,
})
},
&Config{
Protocol: "test",
Server: "test",
},
)
}

func registerValidCommand() {
Expand All @@ -90,7 +95,10 @@ func TestPeriodicCommands(t *testing.T) {
Channels: []string{"#channel"},
CmdFunc: func(channel string) (string, error) { return "ok " + channel, nil },
})
b := New(&Handlers{Response: responseHandler})
b := New(
&Handlers{Response: responseHandler},
&Config{Protocol: "test", Server: "test"},
)
defer b.Close()

entries := b.cron.Entries()
Expand Down Expand Up @@ -123,7 +131,10 @@ func TestMultiplePeriodicCommands(t *testing.T) {
Channels: []string{"#channel"},
CmdFunc: func(channel string) (string, error) { return "ok_afternoon " + channel, nil },
})
b := New(&Handlers{Response: responseHandler})
b := New(
&Handlers{Response: responseHandler},
&Config{Protocol: "test", Server: "test"},
)
defer b.Close()

entries := b.cron.Entries()
Expand Down Expand Up @@ -200,7 +211,9 @@ func TestPeriodicCommandsV2(t *testing.T) {
channel = target
user = sender
replies <- message
}})
}},
&Config{Protocol: "test", Server: "test"})

defer b.Close()

entries := b.cron.Entries()
Expand Down
9 changes: 7 additions & 2 deletions debug/main.go
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"os"

"github.com/go-chat-bot/bot"
bot "github.com/bnfinet/go-chat-bot"
_ "github.com/go-chat-bot/plugins-br/cnpj"
_ "github.com/go-chat-bot/plugins-br/cotacao"
_ "github.com/go-chat-bot/plugins-br/cpf"
Expand Down Expand Up @@ -41,7 +41,12 @@ func responseHandler(target string, message string, sender *bot.User) {
func main() {
b := bot.New(&bot.Handlers{
Response: responseHandler,
})
},
&bot.Config{
Protocol: "debug",
Server: "debug",
},
)

fmt.Println("Type a command or !help for available commands...")

Expand Down
20 changes: 14 additions & 6 deletions google-chat/google-chat.go
Expand Up @@ -2,20 +2,23 @@ package googlechat

import (
"bytes"
"cloud.google.com/go/pubsub"
"context"
"encoding/json"
"github.com/go-chat-bot/bot"
"golang.org/x/oauth2/google"
"log"
"net/http"
"strings"
"time"

"cloud.google.com/go/pubsub"
"github.com/go-chat-bot/bot"
"golang.org/x/oauth2/google"
)

const (
chatAuthScope = "https://www.googleapis.com/auth/chat.bot"
apiEndpoint = "https://chat.googleapis.com/v1/"
protocol = "googlechat"
server = "chat.google.com"
)

var (
Expand Down Expand Up @@ -103,7 +106,12 @@ func Run(config *Config) {

b = bot.New(&bot.Handlers{
Response: responseHandler,
})
},
&bot.Config{
Protocol: protocol,
Server: server,
},
)

err = sub.Receive(context.Background(),
func(ctx context.Context, m *pubsub.Message) {
Expand Down Expand Up @@ -135,8 +143,8 @@ func Run(config *Config) {
log.Printf("Message: %s\n", msg.Message.ArgumentText)
b.MessageReceived(
&bot.ChannelData{
Protocol: "googlechat",
Server: "chat.google.com",
Protocol: protocol,
Server: server,
HumanName: msg.Space.DisplayName,
Channel: msg.Space.Name + ":" + msg.Message.Thread.Name,
IsPrivate: msg.Space.Type == "DM",
Expand Down
15 changes: 11 additions & 4 deletions irc/irc.go
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"strings"

"github.com/go-chat-bot/bot"
bot "github.com/bnfinet/go-chat-bot"
ircevent "github.com/thoj/go-ircevent"
)

Expand All @@ -29,6 +29,8 @@ var (
b *bot.Bot
)

const protocol = "irc"

func responseHandler(target string, message string, sender *bot.User) {
channel := target
if ircConn.GetNick() == target {
Expand All @@ -45,7 +47,7 @@ func responseHandler(target string, message string, sender *bot.User) {
func onPRIVMSG(e *ircevent.Event) {
b.MessageReceived(
&bot.ChannelData{
Protocol: "irc",
Protocol: protocol,
Server: ircConn.Server,
Channel: e.Arguments[0],
IsPrivate: e.Arguments[0] == ircConn.GetNick()},
Expand All @@ -61,7 +63,7 @@ func onPRIVMSG(e *ircevent.Event) {
func onCTCPACTION(e *ircevent.Event) {
b.MessageReceived(
&bot.ChannelData{
Protocol: "irc",
Protocol: protocol,
Server: ircConn.Server,
Channel: e.Arguments[0],
IsPrivate: false,
Expand Down Expand Up @@ -108,7 +110,12 @@ func SetUp(c *Config) *bot.Bot {

b = bot.New(&bot.Handlers{
Response: responseHandler,
})
},
&bot.Config{
Protocol: protocol,
Server: c.Server,
},
)

ircConn.AddCallback("001", onWelcome)
ircConn.AddCallback("PRIVMSG", onPRIVMSG)
Expand Down
16 changes: 13 additions & 3 deletions rocket/rocket.go
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

"github.com/go-chat-bot/bot"
bot "github.com/bnfinet/go-chat-bot"
"github.com/pyinx/gorocket/api"
"github.com/pyinx/gorocket/rest"
)
Expand All @@ -14,6 +14,10 @@ var (
config *Config
)

const (
protocol = "rocket"
)

// Config must contain the necessary data to connect to an rocket.chat server
type Config struct {
Server string
Expand Down Expand Up @@ -55,7 +59,13 @@ func Run(c *Config) {

b := bot.New(&bot.Handlers{
Response: responseHandler,
})
},
&bot.Config{
Protocol: protocol,
Server: config.Server,
},
)

b.Disable([]string{"url"})

msgChan := client.GetAllMessages()
Expand All @@ -66,7 +76,7 @@ func Run(c *Config) {
if !ownMessage(c, msg) {
b.MessageReceived(
&bot.ChannelData{
Protocol: "rocket",
Protocol: protocol,
Server: "",
Channel: msg.ChannelId,
IsPrivate: false,
Expand Down
11 changes: 9 additions & 2 deletions slack/slack.go
Expand Up @@ -4,7 +4,7 @@ package slack
import (
"fmt"

"github.com/go-chat-bot/bot"
bot "github.com/bnfinet/go-chat-bot"
"github.com/nlopes/slack"
)

Expand All @@ -23,6 +23,8 @@ var (
botUserID = ""
)

const protocol = "slack"

func defaultMessageFilter(message string, _ *bot.User) (string, slack.PostMessageParameters) {
return message, params
}
Expand Down Expand Up @@ -125,7 +127,12 @@ func Run(token string) {

b := bot.New(&bot.Handlers{
Response: responseHandler,
})
},
&bot.Config{
Protocol: protocol,
Server: teaminfo.Domain,
},
)

b.Disable([]string{"url"})

Expand Down
20 changes: 16 additions & 4 deletions telegram/telegram.go
Expand Up @@ -6,14 +6,19 @@ import (
"strconv"
"strings"

"github.com/go-chat-bot/bot"
bot "github.com/bnfinet/go-chat-bot"
tgbotapi "gopkg.in/telegram-bot-api.v3"
)

var (
tg *tgbotapi.BotAPI
)

const (
protocol = "telegram"
server = "telegram"
)

func responseHandler(target string, message string, sender *bot.User) {
id, err := strconv.ParseInt(target, 10, 64)
if err != nil {
Expand Down Expand Up @@ -47,13 +52,20 @@ func Run(token string, debug bool) {

b := bot.New(&bot.Handlers{
Response: responseHandler,
})
}, &bot.Config{
Protocol: protocol,
Server: server,
},
)
// b.Protocol = protocol
// b.Server = server

b.Disable([]string{"url"})

for update := range updates {
target := &bot.ChannelData{
Protocol: "telegram",
Server: "telegram",
Protocol: protocol,
Server: server,
Channel: strconv.FormatInt(update.Message.Chat.ID, 10),
IsPrivate: update.Message.Chat.IsPrivate()}
name := []string{update.Message.From.FirstName, update.Message.From.LastName}
Expand Down

0 comments on commit b8f4369

Please sign in to comment.