Moved translation client from bridge to router.
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
prefixed "github.com/matterbridge/logrus-prefixed-formatter"
|
prefixed "github.com/matterbridge/logrus-prefixed-formatter"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"cloud.google.com/go/translate"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -169,6 +170,7 @@ type ConfigValues struct {
|
|||||||
Zulip map[string]Protocol
|
Zulip map[string]Protocol
|
||||||
General Protocol
|
General Protocol
|
||||||
Gateway []Gateway
|
Gateway []Gateway
|
||||||
|
GTClient *translate.Client
|
||||||
SameChannelGateway []SameChannelGateway
|
SameChannelGateway []SameChannelGateway
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package gateway
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
b64 "encoding/base64"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -34,8 +33,6 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
// "github.com/davecgh/go-spew/spew"
|
// "github.com/davecgh/go-spew/spew"
|
||||||
"cloud.google.com/go/translate"
|
"cloud.google.com/go/translate"
|
||||||
"google.golang.org/api/option"
|
|
||||||
"golang.org/x/oauth2/google"
|
|
||||||
"golang.org/x/text/language"
|
"golang.org/x/text/language"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -56,7 +53,6 @@ type Gateway struct {
|
|||||||
Message chan config.Message
|
Message chan config.Message
|
||||||
Name string
|
Name string
|
||||||
Messages *lru.Cache
|
Messages *lru.Cache
|
||||||
GTClient *translate.Client
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type BrMsgID struct {
|
type BrMsgID struct {
|
||||||
@@ -93,18 +89,6 @@ func New(cfg config.Gateway, r *Router) *Gateway {
|
|||||||
cache, _ := lru.New(5000)
|
cache, _ := lru.New(5000)
|
||||||
gw.Messages = cache
|
gw.Messages = cache
|
||||||
gw.AddConfig(&cfg)
|
gw.AddConfig(&cfg)
|
||||||
|
|
||||||
var err error
|
|
||||||
ctx := context.Background()
|
|
||||||
credsJson, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_BASE64"))
|
|
||||||
creds, _ := google.CredentialsFromJSON(ctx, credsJson, translate.Scope)
|
|
||||||
gw.GTClient, err = translate.NewClient(ctx, option.WithCredentials(creds))
|
|
||||||
if err != nil {
|
|
||||||
flog.Warnf("Google Translate API failed to authorize: " + err.Error())
|
|
||||||
} else {
|
|
||||||
flog.Infof("Google Translation enabled.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return gw
|
return gw
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,7 +330,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
|
|||||||
msg.Avatar = gw.modifyAvatar(origmsg, dest)
|
msg.Avatar = gw.modifyAvatar(origmsg, dest)
|
||||||
msg.Username = gw.modifyUsername(origmsg, dest)
|
msg.Username = gw.modifyUsername(origmsg, dest)
|
||||||
msg.ID = ""
|
msg.ID = ""
|
||||||
if (gw.GTClient != nil) && (channel.Options.Locale != "") {
|
if (gw.Router.GTClient != nil) && (channel.Options.Locale != "") {
|
||||||
|
|
||||||
attribution, ok := os.LookupEnv("GOOGLE_TRANSLATE_ATTRIBUTION")
|
attribution, ok := os.LookupEnv("GOOGLE_TRANSLATE_ATTRIBUTION")
|
||||||
if !(ok) {
|
if !(ok) {
|
||||||
@@ -356,7 +340,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
lang, _ := language.Parse(channel.Options.Locale)
|
lang, _ := language.Parse(channel.Options.Locale)
|
||||||
|
|
||||||
client := gw.GTClient
|
client := gw.Router.GTClient
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
text := msg.Text
|
text := msg.Text
|
||||||
|
|||||||
@@ -1,12 +1,18 @@
|
|||||||
package gateway
|
package gateway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
b64 "encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge"
|
"github.com/42wim/matterbridge/bridge"
|
||||||
"github.com/42wim/matterbridge/bridge/config"
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
samechannelgateway "github.com/42wim/matterbridge/gateway/samechannel"
|
samechannelgateway "github.com/42wim/matterbridge/gateway/samechannel"
|
||||||
// "github.com/davecgh/go-spew/spew"
|
// "github.com/davecgh/go-spew/spew"
|
||||||
|
"cloud.google.com/go/translate"
|
||||||
|
"google.golang.org/api/option"
|
||||||
|
"golang.org/x/oauth2/google"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,6 +27,17 @@ func NewRouter(cfg *config.Config) (*Router, error) {
|
|||||||
sgw := samechannelgateway.New(cfg)
|
sgw := samechannelgateway.New(cfg)
|
||||||
gwconfigs := sgw.GetConfig()
|
gwconfigs := sgw.GetConfig()
|
||||||
|
|
||||||
|
var err error
|
||||||
|
ctx := context.Background()
|
||||||
|
credsJson, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_BASE64"))
|
||||||
|
creds, _ := google.CredentialsFromJSON(ctx, credsJson, translate.Scope)
|
||||||
|
r.GTClient, err = translate.NewClient(ctx, option.WithCredentials(creds))
|
||||||
|
if err != nil {
|
||||||
|
flog.Warnf("Google Translate API failed to authorize: " + err.Error())
|
||||||
|
} else {
|
||||||
|
flog.Infof("Google Translation enabled.")
|
||||||
|
}
|
||||||
|
|
||||||
for _, entry := range append(gwconfigs, cfg.Gateway...) {
|
for _, entry := range append(gwconfigs, cfg.Gateway...) {
|
||||||
if !entry.Enable {
|
if !entry.Enable {
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user