forked from zolamk/telegram-form-notification-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (53 loc) · 1.37 KB
/
main.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
)
var (
TELEGRAM_BOT_TOKEN string
TELEGRAM_CHAT_ID string
)
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var data url.Values
var err error
if data, err = url.ParseQuery(request.Body); err != nil {
return events.APIGatewayProxyResponse{
StatusCode: 503,
}, nil
}
if data.Get("honeypot") != "" {
return events.APIGatewayProxyResponse{
StatusCode: 403,
}, nil
}
text := fmt.Sprintf("<b>%s</b><pre>\n</pre><b>%s</b><pre>\n</pre><pre>%s</pre>", data.Get("name"), data.Get("email"), data.Get("message"))
if req, err := http.NewRequest("GET", fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", TELEGRAM_BOT_TOKEN), nil); err != nil {
log.Println(err)
return events.APIGatewayProxyResponse{
StatusCode: 503,
}, nil
} else {
q := req.URL.Query()
q.Add("text", text)
q.Add("chat_id", TELEGRAM_CHAT_ID)
q.Add("parse_mode", "html")
req.URL.RawQuery = q.Encode()
client := http.Client{}
if _, err := client.Do(req); err != nil {
log.Println(err)
return events.APIGatewayProxyResponse{
StatusCode: 503,
}, nil
}
}
return events.APIGatewayProxyResponse{
StatusCode: 204,
}, nil
}
func main() {
lambda.Start(handler)
}