summaryrefslogtreecommitdiff
path: root/pkg/twilio/sms.go
blob: 0a7dba88b47d0c837beb1120243ca82c0afb2312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// SPDX-FileCopyrightText: © 2021 Grégoire Duchêne <gduchene@awhk.org>
// SPDX-License-Identifier: ISC

package twilio

import (
	"net/http"
	"time"
)

type SMS struct {
	DateReceived   time.Time
	From, To, Body string
}

type SMSTee struct {
	Chan    chan<- SMS
	Handler http.Handler
}

var _ http.Handler = &SMSTee{}

func (th *SMSTee) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	select {
	case th.Chan <- SMS{
		DateReceived: time.Now(),
		From:         r.FormValue("From"),
		To:           r.FormValue("To"),
		Body:         r.FormValue("Body"),
	}:
		th.Handler.ServeHTTP(w, r)
	case <-r.Context().Done():
	}
}