From 7d499982c40b555875af2e0e7806d284b1821ccd Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Mon, 22 Nov 2021 16:15:41 -0500 Subject: [PATCH] Remove chatbase support --- backends/rapidpro/backend.go | 16 --------- chatbase/chatbase.go | 50 ---------------------------- chatbase/chatbase_test.go | 64 ------------------------------------ 3 files changed, 130 deletions(-) delete mode 100644 chatbase/chatbase.go delete mode 100644 chatbase/chatbase_test.go diff --git a/backends/rapidpro/backend.go b/backends/rapidpro/backend.go index 3125bf374..da6df33c6 100644 --- a/backends/rapidpro/backend.go +++ b/backends/rapidpro/backend.go @@ -17,7 +17,6 @@ import ( "github.com/jmoiron/sqlx" "github.com/nyaruka/courier" "github.com/nyaruka/courier/batch" - "github.com/nyaruka/courier/chatbase" "github.com/nyaruka/courier/queue" "github.com/nyaruka/courier/utils" "github.com/nyaruka/gocommon/storage" @@ -33,11 +32,6 @@ const msgQueueName = "msgs" // the name of our set for tracking sends const sentSetName = "msgs_sent_%s" -// constants used in org configs for chatbase -const chatbaseAPIKey = "CHATBASE_API_KEY" -const chatbaseVersion = "CHATBASE_VERSION" -const chatbaseMessageType = "agent" - // our timeout for backend operations const backendTimeout = time.Second * 20 @@ -292,16 +286,6 @@ func (b *backend) MarkOutgoingMsgComplete(ctx context.Context, msg courier.Msg, } } } - - // if this org has chatbase connected, notify chatbase - chatKey, _ := msg.Channel().OrgConfigForKey(chatbaseAPIKey, "").(string) - if chatKey != "" { - chatVersion, _ := msg.Channel().OrgConfigForKey(chatbaseVersion, "").(string) - err := chatbase.SendChatbaseMessage(chatKey, chatVersion, chatbaseMessageType, dbMsg.ContactID_.String(), msg.Channel().Name(), msg.Text(), time.Now().UTC()) - if err != nil { - logrus.WithError(err).WithField("chatbase_api_key", chatKey).WithField("chatbase_version", chatVersion).WithField("msg_id", dbMsg.ID().String()).Error("unable to write chatbase message") - } - } } // WriteMsg writes the passed in message to our store diff --git a/chatbase/chatbase.go b/chatbase/chatbase.go deleted file mode 100644 index e5bca39c9..000000000 --- a/chatbase/chatbase.go +++ /dev/null @@ -1,50 +0,0 @@ -package chatbase - -import ( - "bytes" - "encoding/json" - "net/http" - "time" - - "github.com/nyaruka/courier/utils" -) - -// ChatbaseAPIURL is the URL chatbase API messages will be sent to -var chatbaseAPIURL = "https://chatbase.com/api/message" - -// chatbaseLog is the payload for a chatbase request -type chatbaseLog struct { - Type string `json:"type"` - UserID string `json:"user_id"` - Platform string `json:"platform"` - Message string `json:"message"` - TimeStamp int64 `json:"time_stamp"` - - APIKey string `json:"api_key"` - APIVersion string `json:"version,omitempty"` -} - -// SendChatbaseMessage sends a chatbase message with the passed in api key and message details -func SendChatbaseMessage(apiKey string, apiVersion string, messageType string, userID string, platform string, message string, timestamp time.Time) error { - body := chatbaseLog{ - Type: messageType, - UserID: userID, - Platform: platform, - Message: message, - TimeStamp: timestamp.UnixNano() / int64(time.Millisecond), - - APIKey: apiKey, - APIVersion: apiVersion, - } - - jsonBody, err := json.Marshal(body) - if err != nil { - return err - } - - req, _ := http.NewRequest(http.MethodPost, chatbaseAPIURL, bytes.NewReader(jsonBody)) - req.Header.Set("Content-Type", "application/json") - - _, err = utils.MakeHTTPRequest(req) - return err -} diff --git a/chatbase/chatbase_test.go b/chatbase/chatbase_test.go deleted file mode 100644 index 12ee9dbe7..000000000 --- a/chatbase/chatbase_test.go +++ /dev/null @@ -1,64 +0,0 @@ -package chatbase - -import ( - "bytes" - "io/ioutil" - "net/http" - "net/http/httptest" - "testing" - "time" - - "github.com/buger/jsonparser" - "github.com/stretchr/testify/assert" -) - -func TestChatbase(t *testing.T) { - var testRequest *http.Request - var statusCode = 200 - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - body, _ := ioutil.ReadAll(r.Body) - testRequest = httptest.NewRequest(r.Method, r.URL.String(), bytes.NewBuffer(body)) - testRequest.Header = r.Header - w.WriteHeader(statusCode) - w.Write([]byte("ok")) - })) - defer server.Close() - - chatbaseAPIURL = server.URL - - now := time.Now() - err := SendChatbaseMessage("apiKey", "apiVersion", "messageType", "userID", "platform", "message", now) - assert.NoError(t, err) - - // parse our body - bytes, err := ioutil.ReadAll(testRequest.Body) - assert.NoError(t, err) - - // check our request body - str, err := jsonparser.GetString(bytes, "type") - assert.NoError(t, err) - assert.Equal(t, "messageType", str) - - str, err = jsonparser.GetString(bytes, "version") - assert.NoError(t, err) - assert.Equal(t, "apiVersion", str) - - ts, err := jsonparser.GetInt(bytes, "time_stamp") - assert.NoError(t, err) - assert.Equal(t, now.UnixNano()/int64(time.Millisecond), ts) - - // simulate an error - statusCode = 500 - err = SendChatbaseMessage("apiKey", "apiVersion", "messageType", "userID", "platform", "message", now) - assert.Error(t, err) - - // simulate error when messageType is invalid - statusCode = 400 - err = SendChatbaseMessage("apiKey", "apiVersion", "msg", "userID", "platform", "message", now) - assert.Error(t, err) - - bytes, err = ioutil.ReadAll(testRequest.Body) - str, err = jsonparser.GetString(bytes, "type") - assert.NoError(t, err) - assert.Equal(t, "msg", str) -}