-
Notifications
You must be signed in to change notification settings - Fork 54
/
message.go
148 lines (125 loc) · 3.16 KB
/
message.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package tbot
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/yanzay/tbot/internal/adapter"
)
// MessageVars is a parsed message variables lookup table
type MessageVars map[string]string
// Message is a received message from chat, with parsed variables
type Message struct {
*adapter.Message
Vars MessageVars
}
type Option func(*adapter.Message)
var DisablePreview = func(msg *adapter.Message) {
msg.DisablePreview = true
}
var WithMarkdown = func(msg *adapter.Message) {
msg.Markdown = true
}
// Text returns message text
func (m *Message) Text() string {
return m.Data
}
// Reply to the user with plain text
func (m *Message) Reply(reply string, options ...Option) {
msg := &adapter.Message{
ChatID: m.ChatID,
Type: adapter.MessageText,
Data: reply,
}
for _, option := range options {
option(msg)
}
m.Replies <- msg
}
// Replyf is a formatted reply to the user with plain text, with parameters like in fmt.Printf
func (m *Message) Replyf(reply string, values ...interface{}) {
m.Reply(fmt.Sprintf(reply, values...))
}
// ReplySticker sends sticker to the chat.
func (m *Message) ReplySticker(filepath string) {
msg := &adapter.Message{
Type: adapter.MessageSticker,
Data: filepath,
ChatID: m.ChatID,
}
m.Replies <- msg
}
// ReplyPhoto sends photo to the chat. Has optional caption.
func (m *Message) ReplyPhoto(filepath string, caption ...string) {
msg := &adapter.Message{
Type: adapter.MessagePhoto,
Data: filepath,
ChatID: m.ChatID,
}
if len(caption) > 0 {
msg.Caption = caption[0]
}
m.Replies <- msg
}
// ReplyAudio sends audio file to chat
func (m *Message) ReplyAudio(filepath string) {
msg := &adapter.Message{
Type: adapter.MessageAudio,
Data: filepath,
ChatID: m.ChatID,
}
m.Replies <- msg
}
// ReplyDocument sends generic file (not audio, voice, image) to the chat
func (m *Message) ReplyDocument(filepath string) {
msg := &adapter.Message{
Type: adapter.MessageDocument,
Data: filepath,
ChatID: m.ChatID,
}
m.Replies <- msg
}
type KeyboardOption func(*adapter.Message)
var OneTimeKeyboard = func(msg *adapter.Message) {
msg.OneTimeKeyboard = true
}
func (m *Message) ReplyKeyboard(text string, buttons [][]string, options ...KeyboardOption) {
msg := &adapter.Message{
Type: adapter.MessageKeyboard,
Data: text,
Buttons: buttons,
ChatID: m.ChatID,
}
for _, option := range options {
option(msg)
}
m.Replies <- msg
}
// Download file from FileHandler
func (m *Message) Download(dir string) error {
if m.Type != adapter.MessageDocument {
return fmt.Errorf("Nothing to download")
}
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.Mkdir(dir, 0755)
}
tokens := strings.Split(m.Vars["url"], "/")
fileName := tokens[len(tokens)-1]
file, err := os.Create(filepath.Join(dir, fileName))
if err != nil {
return fmt.Errorf("Error creating file: %q", err)
}
defer file.Close()
resp, err := http.Get(m.Vars["url"])
if err != nil {
return fmt.Errorf("Error downloading from %s: %q", m.Vars["url"], err)
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return fmt.Errorf("[Tbot] Error downloading file: %q", err)
}
return nil
}