Initialized Google Translation during bridge init.

This commit is contained in:
Patrick Connolly
2018-10-14 14:45:40 +08:00
parent ceb078bd5b
commit 0185e86e74
3 changed files with 16 additions and 6 deletions

View File

@@ -90,8 +90,18 @@ func New(cfg config.Gateway, r *Router) *Gateway {
cache, _ := lru.New(5000)
gw.Messages = cache
gw.AddConfig(&cfg)
var err error
ctx := context.Background()
gw.GTClient, _ = translate.NewClient(ctx)
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
}
@@ -274,8 +284,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
msg.Avatar = gw.modifyAvatar(origmsg, dest)
msg.Username = gw.modifyUsername(origmsg, dest)
msg.ID = ""
credsEnc, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS_BASE64")
if ok && channel.Options.Locale != "" {
if (gw.GTClient != nil) && (channel.Options.Locale != "") {
attribution, ok := os.LookupEnv("GOOGLE_TRANSLATE_ATTRIBUTION")
if !(ok) {
@@ -285,9 +294,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
ctx := context.Background()
lang, _ := language.Parse(channel.Options.Locale)
credsDec, _ := b64.StdEncoding.DecodeString(credsEnc)
creds, _ := google.CredentialsFromJSON(ctx, credsDec, translate.Scope)
client, _ := translate.NewClient(ctx, option.WithCredentials(creds))
client := gw.GTClient
defer client.Close()
text := msg.Text

1
go.mod
View File

@@ -66,6 +66,7 @@ require (
github.com/spf13/viper v0.0.0-20171227194143-aafc9e6bc7b7
github.com/stretchr/testify v0.0.0-20170714215325-05e8a0eda380
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/urakozz/go-emoji v0.0.0-20151023072830-41ef589a5285
github.com/valyala/bytebufferpool v0.0.0-20160817181652-e746df99fe4a // indirect
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 // indirect
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect

2
go.sum
View File

@@ -153,6 +153,8 @@ github.com/stretchr/testify v0.0.0-20170714215325-05e8a0eda380 h1:MsolbevHkd4Spb
github.com/stretchr/testify v0.0.0-20170714215325-05e8a0eda380/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
github.com/urakozz/go-emoji v0.0.0-20151023072830-41ef589a5285 h1:zsUvs7Dg5tDVBwGbxY+6Hal2PSEH7p6vOiM/g9h2iW4=
github.com/urakozz/go-emoji v0.0.0-20151023072830-41ef589a5285/go.mod h1:esxJGKpQ1iOwOuX/CGCm7zpH/NasLh4P/UIKWIq4NY8=
github.com/valyala/bytebufferpool v0.0.0-20160817181652-e746df99fe4a h1:AOcehBWpFhYPYw0ioDTppQzgI8pAAahVCiMSKTp9rbo=
github.com/valyala/bytebufferpool v0.0.0-20160817181652-e746df99fe4a/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 h1:gKMu1Bf6QINDnvyZuTaACm9ofY+PRh+5vFz4oxBZeF8=