Refactor APIs to add support for inline keyboards (not complete though)

This commit is contained in:
Hamcha 2018-12-12 17:30:38 +01:00
parent 0ddd8993c8
commit 10ef1429c3
Signed by: hamcha
GPG key ID: A40413D21021EAEE
4 changed files with 54 additions and 30 deletions

22
api.go
View file

@ -165,17 +165,17 @@ type APIInlineQuery struct {
// APIInlineQueryResultPhoto is an image result for an inline query
type APIInlineQueryResultPhoto struct {
Type string `json:"type"`
ResultID string `json:"id"`
PhotoURL string `json:"photo_url"`
ThumbURL string `json:"thumb_url"`
Width int `json:"photo_width,omitempty"`
Height int `json:"photo_height,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Caption string `json:"caption,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
ReplyMarkup *APIInlineKeyboardMarkup `json:"reply_markup,omitempty"`
Type string `json:"type"`
ResultID string `json:"id"`
PhotoURL string `json:"photo_url"`
ThumbURL string `json:"thumb_url"`
Width int `json:"photo_width,omitempty"`
Height int `json:"photo_height,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Caption string `json:"caption,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
ReplyMarkup interface{} `json:"reply_markup,omitempty"`
//TODO inputMessageContent
}

View file

@ -35,31 +35,47 @@ func (b *Broker) Close() {
b.Socket.Close()
}
type MessageOptions struct {
ReplyID *int64
ReplyMarkup interface{}
}
// SendTextMessage sends a HTML-styles text message to a chat.
// A reply_to message ID can be specified as optional parameter.
func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int64) {
func (b *Broker) SendTextMessage(chat *APIChat, text string, extra *MessageOptions) {
msgdata := &ClientTextMessageData{
Text: text,
ChatID: chat.ChatID,
}
if extra != nil {
msgdata.ReplyID = extra.ReplyID
msgdata.ReplyMarkup = extra.ReplyMarkup
}
b.sendCmd(ClientCommand{
Type: CmdSendTextMessage,
TextMessageData: &ClientTextMessageData{
Text: text,
ChatID: chat.ChatID,
ReplyID: original,
},
Type: CmdSendTextMessage,
TextMessageData: msgdata,
})
}
// SendPhoto sends a photo with an optional caption to a chat.
// A reply_to message ID can be specified as optional parameter.
func (b *Broker) SendPhoto(chat *APIChat, data []byte, filename string, caption string, original *int64) {
func (b *Broker) SendPhoto(chat *APIChat, data []byte, filename string, caption string, extra *MessageOptions) {
photodata := &ClientPhotoData{
ChatID: chat.ChatID,
Filename: filename,
Bytes: base64.StdEncoding.EncodeToString(data),
Caption: caption,
}
if extra != nil {
photodata.ReplyID = extra.ReplyID
}
b.sendCmd(ClientCommand{
Type: CmdSendPhoto,
PhotoData: &ClientPhotoData{
ChatID: chat.ChatID,
Filename: filename,
Bytes: base64.StdEncoding.EncodeToString(data),
Caption: caption,
ReplyID: original,
},
Type: CmdSendPhoto,
PhotoData: photodata,
})
}

View file

@ -51,9 +51,10 @@ const (
// ClientTextMessageData is the required data for a CmdSendTextMessage request
type ClientTextMessageData struct {
ChatID int64
Text string
ReplyID *int64 `json:",omitempty"`
ChatID int64
Text string
ReplyID *int64 `json:",omitempty"`
ReplyMarkup interface{} `json:",omitempty"`
}
// ClientPhotoData is the required data for a CmdSendPhoto request

View file

@ -87,6 +87,13 @@ func (t Telegram) SendTextMessage(data ClientTextMessageData) {
if data.ReplyID != nil {
postdata["reply_to_message_id"] = []string{strconv.FormatInt(*(data.ReplyID), 10)}
}
if data.ReplyMarkup != nil {
replyjson, err := json.Marshal(data.ReplyMarkup)
if !checkerr("SendTextMessage/json.Marshal", err) {
return
}
postdata["reply_markup"] = []string{string(replyjson)}
}
_, err := http.PostForm(t.apiURL("sendMessage"), postdata)
checkerr("SendTextMessage/http.PostForm", err)