Skip to content

Commit

Permalink
Merge pull request #75 from Ilhasoft/revert-73-feature/botdefense-wha…
Browse files Browse the repository at this point in the history
…tsapp

Revert "Feature/botdefense whatsapp"
  • Loading branch information
matmsa27 authored Sep 3, 2021
2 parents b484058 + 037a07f commit dc629b1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 46 deletions.
6 changes: 2 additions & 4 deletions backends/rapidpro/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ SELECT
c.modified_on,
c.created_on,
c.name,
u.id as "urn_id",
c.status
u.id as "urn_id"
FROM
contacts_contact AS c,
contacts_contacturn AS u
Expand Down Expand Up @@ -233,8 +232,7 @@ type DBContact struct {
CreatedBy_ int `db:"created_by_id"`
ModifiedBy_ int `db:"modified_by_id"`

IsNew_ bool
Status_ string `db:"status"`
IsNew_ bool
}

// UUID returns the UUID for this contact
Expand Down
36 changes: 4 additions & 32 deletions handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/backends/rapidpro"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/rcache"
Expand Down Expand Up @@ -63,7 +62,7 @@ func newWAHandler(channelType courier.ChannelType, name string) courier.ChannelH
// Initialize is called by the engine once everything is loaded
func (h *handler) Initialize(s courier.Server) error {
h.SetServer(s)
s.AddHandlerRoute(h, http.MethodPost, "receive", h.checkBlockedContact)
s.AddHandlerRoute(h, http.MethodPost, "receive", h.receiveEvent)
return nil
}

Expand Down Expand Up @@ -160,8 +159,8 @@ type eventPayload struct {
MimeType string `json:"mime_type" validate:"required"`
Sha256 string `json:"sha256" validate:"required"`
} `json:"voice"`
Contacts []struct {
Phones []struct {
Contacts []struct{
Phones []struct{
Phone string `json:"phone"`
} `json:"phones"`
} `json:"contacts"`
Expand All @@ -174,33 +173,6 @@ type eventPayload struct {
} `json:"statuses"`
}

// checkBlockedContact is a handler middleware to prevent generate channel log from blocked contact messages
func (h *handler) checkBlockedContact(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {

payload := &eventPayload{}
err := handlers.DecodeAndValidateJSON(payload, r)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}

if len(payload.Contacts) > 0 {
if contactURN, err := urns.NewWhatsAppURN(payload.Contacts[0].WaID); err == nil {
if contact, err := h.Backend().GetContact(ctx, channel, contactURN, channel.StringConfigForKey(courier.ConfigAuthToken, ""), ""); err == nil {
c, _ := json.Marshal(contact)
var dbc rapidpro.DBContact
err2 := json.Unmarshal(c, &dbc)
if err2 == nil {
if dbc.Status_ == "B" {
return nil, errors.New("blocked contact sending message")
}
}
}
}
}

return h.receiveEvent(ctx, channel, w, r)
}

// receiveMessage is our HTTP handler function for incoming messages
func (h *handler) receiveEvent(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {
payload := &eventPayload{}
Expand Down Expand Up @@ -273,7 +245,7 @@ func (h *handler) receiveEvent(ctx context.Context, channel courier.Channel, w h
phones = append(phones, phone.Phone)
}
text = strings.Join(phones, ", ")
} else {
}else {
// we received a message type we do not support.
courier.LogRequestError(r, channel, fmt.Errorf("unsupported message type %s", msg.Type))
}
Expand Down
14 changes: 4 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,21 +314,15 @@ func (s *server) channelHandleWrapper(handler ChannelHandler, handlerFunc Channe

// if we received an error, write it out and report it
if err != nil {
if err.Error() != "blocked contact sending message" {
logrus.WithError(err).WithField("channel_uuid", channel.UUID()).WithField("url", url).WithField("request", string(request)).Error("error handling request")
writeAndLogRequestError(ctx, ww, r, channel, err)
}
logrus.WithError(err).WithField("channel_uuid", channel.UUID()).WithField("url", url).WithField("request", string(request)).Error("error handling request")
writeAndLogRequestError(ctx, ww, r, channel, err)
}

// if we have a channel matched but no events were created we still want to log this to the channel, do so
if channel != nil && len(events) == 0 {
if err != nil {
if err.Error() == "blocked contact sending message" {
return
} else {
logs = append(logs, NewChannelLog("Channel Error", channel, NilMsgID, r.Method, url, ww.Status(), string(request), prependHeaders(response.String(), ww.Status(), w), duration, err))
librato.Gauge(fmt.Sprintf("courier.channel_error_%s", channel.ChannelType()), secondDuration)
}
logs = append(logs, NewChannelLog("Channel Error", channel, NilMsgID, r.Method, url, ww.Status(), string(request), prependHeaders(response.String(), ww.Status(), w), duration, err))
librato.Gauge(fmt.Sprintf("courier.channel_error_%s", channel.ChannelType()), secondDuration)
} else {
logs = append(logs, NewChannelLog("Request Ignored", channel, NilMsgID, r.Method, url, ww.Status(), string(request), prependHeaders(response.String(), ww.Status(), w), duration, err))
librato.Gauge(fmt.Sprintf("courier.channel_ignored_%s", channel.ChannelType()), secondDuration)
Expand Down

0 comments on commit dc629b1

Please sign in to comment.