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

View file

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

View file

@ -51,9 +51,10 @@ const (
// ClientTextMessageData is the required data for a CmdSendTextMessage request // ClientTextMessageData is the required data for a CmdSendTextMessage request
type ClientTextMessageData struct { type ClientTextMessageData struct {
ChatID int64 ChatID int64
Text string Text string
ReplyID *int64 `json:",omitempty"` ReplyID *int64 `json:",omitempty"`
ReplyMarkup interface{} `json:",omitempty"`
} }
// ClientPhotoData is the required data for a CmdSendPhoto request // 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 { if data.ReplyID != nil {
postdata["reply_to_message_id"] = []string{strconv.FormatInt(*(data.ReplyID), 10)} 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) _, err := http.PostForm(t.apiURL("sendMessage"), postdata)
checkerr("SendTextMessage/http.PostForm", err) checkerr("SendTextMessage/http.PostForm", err)