diff --git a/gateway/gateway.go b/gateway/gateway.go index 830a734e..e221b21e 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -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 diff --git a/go.mod b/go.mod index 2749b673..3dbe87dc 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 7964e1cd..5051a21e 100644 --- a/go.sum +++ b/go.sum @@ -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=