-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram.go
More file actions
85 lines (65 loc) · 1.83 KB
/
Copy pathtelegram.go
File metadata and controls
85 lines (65 loc) · 1.83 KB
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 teledisq
import (
"fmt"
"net/url"
"os"
"strings"
"golang.org/x/net/context"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
)
func HandleTelegramUpdate(ctx context.Context, u Update) {
m := &Message{}
if u.EditedMessage != nil {
m = u.EditedMessage
} else {
m = u.Message
}
log.Infof(ctx, "Update message content: %+v", u)
if m.IsCommand() {
handleCommand(ctx, m)
} else {
handleMessage(ctx, m)
}
}
func handleCommand(ctx context.Context, m *Message) {
}
func handleMessage(ctx context.Context, m *Message) {
}
func SendMessage(ctx context.Context, chat int64, text string) {
SendFormattedMessage(ctx, chat, text, "")
}
func SendFormattedMessage(ctx context.Context, chat int64, text string, format string) {
payload := make(url.Values)
payload.Add("chat_id", fmt.Sprintf("%d", chat))
payload.Add("text", sanitizeHTMLInput(text))
if format != "" {
payload.Add("parse_mode", format)
}
makeRequest(ctx, CommandSendMessage, payload)
}
func makeRequest(ctx context.Context, cmd string, data url.Values) {
c := urlfetch.Client(ctx)
if c == nil {
log.Errorf(ctx, "Can't create AppEngine urlfetch Client")
return
}
address := fmt.Sprintf("https://api.telegram.org/bot%s/%s", os.Getenv("TELEGRAM_SECRET"), cmd)
// Always add the telegram method we use to POST
data.Add("method", cmd)
resp, err := c.PostForm(address, data)
if err != nil {
log.Errorf(ctx, "Fail to make send message request %s. Payload: %#v", cmd, data)
return
}
if resp.StatusCode > 201 {
log.Errorf(ctx, "Bad send message request for '%s'.\nStatus: %s\nPayload: %#v", cmd, resp.Status, data)
return
}
}
func sanitizeHTMLInput(text string) string {
text = strings.Replace(text, "<p>", " ", -1)
text = strings.Replace(text, "</p>", " ", -1)
text = strings.Replace(text, "\\\"", "\"", -1)
return text
}