-
Notifications
You must be signed in to change notification settings - Fork 2
/
type.go
182 lines (161 loc) · 4.41 KB
/
type.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package bot
import (
"context"
"encoding/json"
"fmt"
"io"
"time"
)
// Client represent a chat client. Currently supports telegram
type Client interface {
// AddPlugins will add each plugin as middleware, each message will flow to the Handle for each plugin unless
// previous plugin return handled equals true
AddPlugins(...Plugin) error
// Start listening for new messages and block
Start(context.Context) error
// UserName of the bot
UserName() string
//Mentioned will return true filed is a mention to a user
Mentioned(field string) bool
// Mention a user
Mention(user User) string
// UserByName find user by username
UserByName(username string) (User, bool)
// Get chat information such as topic etc
ChatInfo(ctx context.Context, chatID string) (ChatInfo, error)
// SetTopic for a channel
SetTopic(ctx context.Context, chatID, topic string) error
// UploadFile to a channel
UploadFile(ctx context.Context, chatID string, filename string, r io.Reader) error
// Get all replies to a threaded message
ThreadReplies(ctx context.Context, chat Chat, threadID string) ([]*Message, error)
// Get Permanent link of a message
MessagePermalink(ctx context.Context, msg *Message) (string, error)
// Fetch Private Image that needs authentication
FetchImage(ctx context.Context, fileURL string) (io.ReadCloser, error)
}
type Attachment struct {
Fallback string `json:"fallback"`
Text string `json:"text"`
Pretext string `json:"pretext"`
Title string `json:"title"`
ID int64 `json:"id"`
}
// Message represents chat message
type Message struct {
ctx context.Context
ID string
From User
Date time.Time
Chat Chat
Text string
Format MessageFormat
ReplyTo *Message
ReplyToID string
ReceivedAt time.Time
Attachments []Attachment
Files []File
PreviousMessage *Message // if message was edited, this is original message
Raw json.RawMessage `json:"-"`
Retry int `json:"-"`
DiscardAfter time.Time `json:"-"`
}
type File struct {
Id string
Name string
Mimetype string
Filetype string
Url string
UrlPublic string
To []FileAddress
From []FileAddress
PlainText string `json:"plain_text"`
}
type FileAddress struct {
Address string `json:"address"`
Name string `json:"name"`
Original string `json:"original"`
}
func (m *Message) Context() context.Context {
return m.ctx
}
func (m *Message) WithContext(ctx context.Context) *Message {
if ctx == nil {
panic("nil context")
}
m2 := new(Message)
m2 = m
m2.ctx = ctx
return m2
}
// JoinMessage represents information that a user join a chat
type JoinMessage struct {
*Message
}
// LeftMessage represents information that a user left a chat
type LeftMessage struct {
*Message
}
// ChannelMigratedMessage represents that a chat type has been upgraded. Currently works on telegram
type ChannelMigratedMessage struct {
FromID string
ToID string
ReceivedAt time.Time
Raw json.RawMessage `json:"-"`
}
// MessageFormat represents formatting of the message
type MessageFormat string
// Available MessageFormat
const (
Text MessageFormat = ""
Markdown MessageFormat = "markdown"
HTML MessageFormat = "html"
)
// User represents user information
type User struct {
ID string
FirstName string
LastName string
Username string
}
// FullName returns first name + last name
func (u User) FullName() string {
if u.LastName != "" {
return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
}
return u.FirstName
}
// ChatType is type of the message
type ChatType string
// Available ChatType
const (
Channel ChatType = "channel"
Group ChatType = "group"
Private ChatType = "private"
SuperGroup ChatType = "supergroup"
Thread ChatType = "thread"
)
// Chat represents a chat session
type Chat struct {
ID string
Type ChatType
Title string
Username string
}
type ChatInfo struct {
ID string
Type ChatType
Title string
Topic string
Description string
}
// Name returns the title of the bot
func (t Chat) Name() string {
return fmt.Sprintf(" (@%s)", t.Username)
}
// Plugin is pluggable module to process messages
type Plugin interface {
Name() string
Init(ctx context.Context, out chan Message, cl Client) error
Handle(ctx context.Context, in interface{}) (handled bool, msg interface{})
}