From 86ed4ead48a45dc5175df0243c17b9d38134fbd1 Mon Sep 17 00:00:00 2001 From: Anton Filimonov Date: Sun, 5 Nov 2017 03:02:14 +0300 Subject: [PATCH 01/11] Allow to pass custom http client --- internal/adapter/adapter.go | 6 +++--- message.go | 2 +- message_processor.go | 2 +- server.go | 20 ++++++++++++++------ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/internal/adapter/adapter.go b/internal/adapter/adapter.go index 04eec7d..7ffb3c1 100644 --- a/internal/adapter/adapter.go +++ b/internal/adapter/adapter.go @@ -8,7 +8,7 @@ import ( "strings" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" - "github.com/yanzay/tbot/model" + "github.com/variar/tbot/model" ) type BotAdapter interface { @@ -22,8 +22,8 @@ type Bot struct { tbot *tgbotapi.BotAPI } -func CreateBot(token string) (BotAdapter, error) { - tbot, err := tgbotapi.NewBotAPI(token) +func CreateBot(token string, httpClient *http.Client) (BotAdapter, error) { + tbot, err := tgbotapi.NewBotAPIWithClient(token, httpClient) if err != nil { return nil, err } diff --git a/message.go b/message.go index 86bc7cc..54e8137 100644 --- a/message.go +++ b/message.go @@ -8,7 +8,7 @@ import ( "path/filepath" "strings" - "github.com/yanzay/tbot/model" + "github.com/variar/tbot/model" ) // MessageVars is a parsed message variables lookup table diff --git a/message_processor.go b/message_processor.go index 18fb43f..225f548 100644 --- a/message_processor.go +++ b/message_processor.go @@ -3,8 +3,8 @@ package tbot import ( "strings" + "github.com/variar/tbot/model" "github.com/yanzay/log" - "github.com/yanzay/tbot/model" ) func (s *Server) processMessage(message *Message) { diff --git a/server.go b/server.go index c148698..d7fed0b 100644 --- a/server.go +++ b/server.go @@ -1,8 +1,10 @@ package tbot import ( - "github.com/yanzay/tbot/internal/adapter" - "github.com/yanzay/tbot/model" + "net/http" + + "github.com/variar/tbot/internal/adapter" + "github.com/variar/tbot/model" ) // Server is a telegram bot server. Looks and feels like net/http. @@ -18,8 +20,8 @@ type Server struct { // Should call it's argument function inside, if needed. type Middleware func(HandlerFunction) HandlerFunction -var createBot = func(token string) (adapter.BotAdapter, error) { - return adapter.CreateBot(token) +var createBot = func(token string, httpClient *http.Client) (adapter.BotAdapter, error) { + return adapter.CreateBot(token, httpClient) } // ServerOption is a functional option for Server @@ -42,9 +44,15 @@ func WithMux(m Mux) ServerOption { } // NewServer creates new Server with Telegram API Token -// and default /help handler +// and default /help handler using go default http client func NewServer(token string, options ...ServerOption) (*Server, error) { - tbot, err := createBot(token) + return NewServerWithClient(token, http.DefaultClient, options...) +} + +// NewServerWithClient creates new Server with Telegram API Token +// and default /help handler +func NewServerWithClient(token string, httpClient *http.Client, options ...ServerOption) (*Server, error) { + tbot, err := createBot(token, httpClient) if err != nil { return nil, err } From ab76d834d81ee17886ee9413075cba986ae02d79 Mon Sep 17 00:00:00 2001 From: Anton Filimonov Date: Sun, 5 Nov 2017 13:53:13 +0300 Subject: [PATCH 02/11] Allow custom http client --- internal/adapter/adapter.go | 2 +- message.go | 2 +- message_processor.go | 2 +- server.go | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/adapter/adapter.go b/internal/adapter/adapter.go index 7ffb3c1..02e2af8 100644 --- a/internal/adapter/adapter.go +++ b/internal/adapter/adapter.go @@ -8,7 +8,7 @@ import ( "strings" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" - "github.com/variar/tbot/model" + "github.com/yanzay/tbot/model" ) type BotAdapter interface { diff --git a/message.go b/message.go index 54e8137..86bc7cc 100644 --- a/message.go +++ b/message.go @@ -8,7 +8,7 @@ import ( "path/filepath" "strings" - "github.com/variar/tbot/model" + "github.com/yanzay/tbot/model" ) // MessageVars is a parsed message variables lookup table diff --git a/message_processor.go b/message_processor.go index 225f548..18fb43f 100644 --- a/message_processor.go +++ b/message_processor.go @@ -3,8 +3,8 @@ package tbot import ( "strings" - "github.com/variar/tbot/model" "github.com/yanzay/log" + "github.com/yanzay/tbot/model" ) func (s *Server) processMessage(message *Message) { diff --git a/server.go b/server.go index d7fed0b..05f3e49 100644 --- a/server.go +++ b/server.go @@ -3,8 +3,8 @@ package tbot import ( "net/http" - "github.com/variar/tbot/internal/adapter" - "github.com/variar/tbot/model" + "github.com/yanzay/tbot/internal/adapter" + "github.com/yanzay/tbot/model" ) // Server is a telegram bot server. Looks and feels like net/http. From 708b61dca33699bab541c60419580dc7bd579280 Mon Sep 17 00:00:00 2001 From: Anton Filimonov Date: Sun, 5 Nov 2017 14:03:55 +0300 Subject: [PATCH 03/11] use new function signature in tests --- main_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main_test.go b/main_test.go index 58fb5a6..9ab805e 100644 --- a/main_test.go +++ b/main_test.go @@ -2,6 +2,7 @@ package tbot import ( "fmt" + "net/http" "os" "testing" @@ -10,7 +11,7 @@ import ( ) func TestMain(m *testing.M) { - createBot = func(token string) (adapter.BotAdapter, error) { + createBot = func(token string, httpClient *http.Client) (adapter.BotAdapter, error) { if token == TestToken { return &mockBot{}, nil } From 8a329c950f19212225d616b548c81c8f9fec7350 Mon Sep 17 00:00:00 2001 From: Anton Filimonov Date: Tue, 7 Nov 2017 15:27:50 +0300 Subject: [PATCH 04/11] Use ServerOption style to pass http client --- server.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/server.go b/server.go index 05f3e49..503b3d2 100644 --- a/server.go +++ b/server.go @@ -9,11 +9,12 @@ import ( // Server is a telegram bot server. Looks and feels like net/http. type Server struct { - bot adapter.BotAdapter mux Mux + httpClient *http.Client middlewares []Middleware webhookURL string listenAddr string + bot adapter.BotAdapter } // Middleware function takes HandlerFunction and returns HandlerFunction. @@ -43,29 +44,31 @@ func WithMux(m Mux) ServerOption { } } +// WithHttpClient sets custom http client for server. +func WithHttpClient(client *http.Client) ServerOption { + return func(s *Server) { + s.httpClient = client + } +} + // NewServer creates new Server with Telegram API Token // and default /help handler using go default http client func NewServer(token string, options ...ServerOption) (*Server, error) { - return NewServerWithClient(token, http.DefaultClient, options...) -} - -// NewServerWithClient creates new Server with Telegram API Token -// and default /help handler -func NewServerWithClient(token string, httpClient *http.Client, options ...ServerOption) (*Server, error) { - tbot, err := createBot(token, httpClient) - if err != nil { - return nil, err - } - server := &Server{ - bot: tbot, - mux: NewDefaultMux(), + mux: NewDefaultMux(), + httpClient: http.DefaultClient, } for _, option := range options { option(server) } + tbot, err := createBot(token, server.httpClient) + if err != nil { + return nil, err + } + server.bot = tbot + server.HandleFunc("/help", server.HelpHandler) return server, nil From f30dc2f4ac9034b53b6b4619667d997dc6810853 Mon Sep 17 00:00:00 2001 From: eeonevision Date: Sun, 28 Jan 2018 17:40:03 +0300 Subject: [PATCH 05/11] Added support for Contact and Location buttons. --- .gitignore | 2 ++ default_mux_test.go | 2 +- examples/cowsay/main.go | 2 +- examples/deep_thought/main.go | 2 +- examples/simple/main.go | 2 +- internal/adapter/adapter.go | 41 ++++++++++++++++++++++++++++++++++- main_test.go | 6 ++--- message.go | 2 +- message_processor.go | 2 +- message_test.go | 2 +- model/message.go | 27 +++++++++++++++++++++++ router_mux_test.go | 2 +- server.go | 4 ++-- 13 files changed, 82 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index c7ab203..235096e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ uploads *.swp +.DS_Store +.vscode \ No newline at end of file diff --git a/default_mux_test.go b/default_mux_test.go index 7ea8d47..75200f6 100644 --- a/default_mux_test.go +++ b/default_mux_test.go @@ -3,7 +3,7 @@ package tbot import ( "testing" - "github.com/yanzay/tbot/model" + "github.com/eeonevision/tbot/model" ) func TestDefaultMux(t *testing.T) { diff --git a/examples/cowsay/main.go b/examples/cowsay/main.go index 66228d2..abb1efb 100644 --- a/examples/cowsay/main.go +++ b/examples/cowsay/main.go @@ -7,7 +7,7 @@ import ( "strings" "unicode/utf8" - "github.com/yanzay/tbot" + "github.com/eeonevision/tbot" ) func main() { diff --git a/examples/deep_thought/main.go b/examples/deep_thought/main.go index 164bb5f..87df17d 100644 --- a/examples/deep_thought/main.go +++ b/examples/deep_thought/main.go @@ -6,7 +6,7 @@ import ( "strconv" "time" - "github.com/yanzay/tbot" + "github.com/eeonevision/tbot" ) func main() { diff --git a/examples/simple/main.go b/examples/simple/main.go index e8f640a..ad28039 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -5,7 +5,7 @@ import ( "os" "time" - "github.com/yanzay/tbot" + "github.com/eeonevision/tbot" ) func main() { diff --git a/internal/adapter/adapter.go b/internal/adapter/adapter.go index 02e2af8..1754e10 100644 --- a/internal/adapter/adapter.go +++ b/internal/adapter/adapter.go @@ -7,8 +7,8 @@ import ( "net/url" "strings" + "github.com/eeonevision/tbot/model" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" - "github.com/yanzay/tbot/model" ) type BotAdapter interface { @@ -99,6 +99,20 @@ func (b *Bot) adaptUpdates(updates <-chan tgbotapi.Update, messages chan<- *mode LanguageCode: updateMessage.From.LanguageCode, } } + if updateMessage.Contact != nil { + message.Contact = model.Contact{ + PhoneNumber: updateMessage.Contact.PhoneNumber, + FirstName: updateMessage.Contact.FirstName, + LastName: updateMessage.Contact.LastName, + UserID: updateMessage.Contact.UserID, + } + } + if updateMessage.Location != nil { + message.Location = model.Location{ + Longitude: updateMessage.Location.Longitude, + Latitude: updateMessage.Location.Latitude, + } + } switch { case updateMessage.Document != nil: message.Type = model.MessageDocument @@ -149,6 +163,16 @@ func chattableFromMessage(m *model.Message) tgbotapi.Chattable { msg.ParseMode = tgbotapi.ModeMarkdown } return msg + case model.MessageSpecialKeyboard: + msg := tgbotapi.NewMessage(m.ChatID, m.Data) + btns := buttonsFromSpecialButtons(m.SpecialButtons) + keyboard := tgbotapi.NewReplyKeyboard(btns...) + keyboard.OneTimeKeyboard = m.OneTimeKeyboard + msg.ReplyMarkup = keyboard + if m.Markdown { + msg.ParseMode = tgbotapi.ModeMarkdown + } + return msg } return nil } @@ -164,6 +188,21 @@ func buttonsFromStrings(strs [][]string) [][]tgbotapi.KeyboardButton { return btns } +func buttonsFromSpecialButtons(sbtns [][]model.KeyboardButton) [][]tgbotapi.KeyboardButton { + btns := make([][]tgbotapi.KeyboardButton, len(sbtns)) + for i, buttonRow := range sbtns { + btns[i] = make([]tgbotapi.KeyboardButton, len(buttonRow)) + for j, button := range buttonRow { + btns[i][j] = tgbotapi.KeyboardButton{ + Text: button.Text, + RequestContact: button.RequestContact, + RequestLocation: button.RequestLocation, + } + } + } + return btns +} + func fileMessage(m *model.Message, file tgbotapi.BaseFile) tgbotapi.BaseFile { if strings.HasPrefix(m.Data, "http") { _, err := url.Parse(m.Data) diff --git a/main_test.go b/main_test.go index 9ab805e..9f26155 100644 --- a/main_test.go +++ b/main_test.go @@ -6,8 +6,8 @@ import ( "os" "testing" - "github.com/yanzay/tbot/internal/adapter" - "github.com/yanzay/tbot/model" + "github.com/eeonevision/tbot/internal/adapter" + "github.com/eeonevision/tbot/model" ) func TestMain(m *testing.M) { @@ -69,7 +69,7 @@ func TestDocumentUpload(t *testing.T) { }) } requestResponse(t, setup, - "https://raw.githubusercontent.com/yanzay/tbot/master/LICENSE", + "https://raw.githubusercontent.com/eeonevision/tbot/master/LICENSE", model.MessageDocument, "OK", model.MessageText) } diff --git a/message.go b/message.go index 86bc7cc..d717121 100644 --- a/message.go +++ b/message.go @@ -8,7 +8,7 @@ import ( "path/filepath" "strings" - "github.com/yanzay/tbot/model" + "github.com/eeonevision/tbot/model" ) // MessageVars is a parsed message variables lookup table diff --git a/message_processor.go b/message_processor.go index 18fb43f..92fc14e 100644 --- a/message_processor.go +++ b/message_processor.go @@ -3,8 +3,8 @@ package tbot import ( "strings" + "github.com/eeonevision/tbot/model" "github.com/yanzay/log" - "github.com/yanzay/tbot/model" ) func (s *Server) processMessage(message *Message) { diff --git a/message_test.go b/message_test.go index 294c485..4a71295 100644 --- a/message_test.go +++ b/message_test.go @@ -3,7 +3,7 @@ package tbot import ( "testing" - "github.com/yanzay/tbot/model" + "github.com/eeonevision/tbot/model" ) func TestReply(t *testing.T) { diff --git a/model/message.go b/model/message.go index b3d51fe..374f16a 100644 --- a/model/message.go +++ b/model/message.go @@ -9,6 +9,7 @@ const ( MessagePhoto MessageAudio MessageKeyboard + MessageSpecialKeyboard ) const ( @@ -24,15 +25,41 @@ type Message struct { Caption string Replies chan *Message From User + Contact Contact + Location Location ChatID int64 ChatType string DisablePreview bool Markdown bool Buttons [][]string + SpecialButtons [][]KeyboardButton OneTimeKeyboard bool ForwardDate int } +// KeyboardButton is a button within a custom keyboard. +type KeyboardButton struct { + Text string `json:"text"` + RequestContact bool `json:"request_contact"` + RequestLocation bool `json:"request_location"` +} + +// Contact contains information about a contact. +// +// Note that LastName and UserID may be empty. +type Contact struct { + PhoneNumber string `json:"phone_number"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` // optional + UserID int `json:"user_id"` // optional +} + +// Location contains information about a place. +type Location struct { + Longitude float64 `json:"longitude"` + Latitude float64 `json:"latitude"` +} + // User is a user on Telegram. type User struct { ID int `json:"id"` diff --git a/router_mux_test.go b/router_mux_test.go index 85a662b..b3ea9c4 100644 --- a/router_mux_test.go +++ b/router_mux_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/yanzay/tbot/model" + "github.com/eeonevision/tbot/model" ) type testSequence struct { diff --git a/server.go b/server.go index 503b3d2..0c6349f 100644 --- a/server.go +++ b/server.go @@ -3,8 +3,8 @@ package tbot import ( "net/http" - "github.com/yanzay/tbot/internal/adapter" - "github.com/yanzay/tbot/model" + "github.com/eeonevision/tbot/internal/adapter" + "github.com/eeonevision/tbot/model" ) // Server is a telegram bot server. Looks and feels like net/http. From ddb10e62f3b3df5459448c8866be537b7910ab67 Mon Sep 17 00:00:00 2001 From: eeonevision Date: Sun, 28 Jan 2018 17:49:49 +0300 Subject: [PATCH 06/11] Added method ReplySpecialKeyboard. --- message.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/message.go b/message.go index d717121..e76b08b 100644 --- a/message.go +++ b/message.go @@ -122,6 +122,20 @@ func (m *Message) ReplyKeyboard(text string, buttons [][]string, options ...Keyb m.sendReply(msg) } +// ReplySpecialKeyboard sends custom reply keyboard to the user with included special buttons. +func (m *Message) ReplySpecialKeyboard(text string, buttons [][]model.KeyboardButton, options ...KeyboardOption) { + msg := &model.Message{ + Type: model.MessageSpecialKeyboard, + Data: text, + SpecialButtons: buttons, + ChatID: m.ChatID, + } + for _, option := range options { + option(msg) + } + m.sendReply(msg) +} + // Download file from FileHandler func (m *Message) Download(dir string) error { if m.Type != model.MessageDocument { From 79bba65636b03dde9b2632848b73a8829389e1b8 Mon Sep 17 00:00:00 2001 From: eeonevision Date: Sun, 28 Jan 2018 22:34:55 +0300 Subject: [PATCH 07/11] Fixed Message contact and location detect --- internal/adapter/adapter.go | 12 ++++++++++++ model/message.go | 2 ++ 2 files changed, 14 insertions(+) diff --git a/internal/adapter/adapter.go b/internal/adapter/adapter.go index 1754e10..fa55bc1 100644 --- a/internal/adapter/adapter.go +++ b/internal/adapter/adapter.go @@ -126,6 +126,12 @@ func (b *Bot) adaptUpdates(updates <-chan tgbotapi.Update, messages chan<- *mode message.Type = model.MessageText message.Data = updateMessage.Text messages <- message + case updateMessage.Contact.PhoneNumber != "": + message.Type = model.MessageContact + messages <- message + case updateMessage.Location != nil: + message.Type = model.MessageLocation + messages <- message } } } @@ -139,6 +145,12 @@ func chattableFromMessage(m *model.Message) tgbotapi.Chattable { msg.ParseMode = tgbotapi.ModeMarkdown } return msg + case model.MessageContact: + msg := tgbotapi.NewContact(m.ChatID, m.Contact.PhoneNumber, m.From.FirstName) + return msg + case model.MessageLocation: + msg := tgbotapi.NewLocation(m.ChatID, m.Location.Latitude, m.Location.Longitude) + return msg case model.MessageSticker: return tgbotapi.NewStickerUpload(m.ChatID, m.Data) case model.MessagePhoto: diff --git a/model/message.go b/model/message.go index 374f16a..04fc0fb 100644 --- a/model/message.go +++ b/model/message.go @@ -4,6 +4,8 @@ type MessageType int const ( MessageText MessageType = iota + MessageContact + MessageLocation MessageDocument MessageSticker MessagePhoto From d80ff699d54b4c9277dc8c1ac98708eaab106cb6 Mon Sep 17 00:00:00 2001 From: eeonevision Date: Mon, 29 Jan 2018 12:51:04 +0300 Subject: [PATCH 08/11] Fixed packages name for successful merge. --- default_mux_test.go | 2 +- examples/cowsay/main.go | 2 +- examples/deep_thought/main.go | 2 +- examples/simple/main.go | 2 +- internal/adapter/adapter.go | 10 +++++----- main_test.go | 6 +++--- message.go | 2 +- message_processor.go | 2 +- message_test.go | 2 +- router_mux_test.go | 2 +- server.go | 4 ++-- 11 files changed, 18 insertions(+), 18 deletions(-) diff --git a/default_mux_test.go b/default_mux_test.go index 75200f6..7ea8d47 100644 --- a/default_mux_test.go +++ b/default_mux_test.go @@ -3,7 +3,7 @@ package tbot import ( "testing" - "github.com/eeonevision/tbot/model" + "github.com/yanzay/tbot/model" ) func TestDefaultMux(t *testing.T) { diff --git a/examples/cowsay/main.go b/examples/cowsay/main.go index abb1efb..66228d2 100644 --- a/examples/cowsay/main.go +++ b/examples/cowsay/main.go @@ -7,7 +7,7 @@ import ( "strings" "unicode/utf8" - "github.com/eeonevision/tbot" + "github.com/yanzay/tbot" ) func main() { diff --git a/examples/deep_thought/main.go b/examples/deep_thought/main.go index 87df17d..164bb5f 100644 --- a/examples/deep_thought/main.go +++ b/examples/deep_thought/main.go @@ -6,7 +6,7 @@ import ( "strconv" "time" - "github.com/eeonevision/tbot" + "github.com/yanzay/tbot" ) func main() { diff --git a/examples/simple/main.go b/examples/simple/main.go index ad28039..e8f640a 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -5,7 +5,7 @@ import ( "os" "time" - "github.com/eeonevision/tbot" + "github.com/yanzay/tbot" ) func main() { diff --git a/internal/adapter/adapter.go b/internal/adapter/adapter.go index fa55bc1..4d473da 100644 --- a/internal/adapter/adapter.go +++ b/internal/adapter/adapter.go @@ -7,8 +7,8 @@ import ( "net/url" "strings" - "github.com/eeonevision/tbot/model" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/yanzay/tbot/model" ) type BotAdapter interface { @@ -122,16 +122,16 @@ func (b *Bot) adaptUpdates(updates <-chan tgbotapi.Update, messages chan<- *mode continue } messages <- message - case updateMessage.Text != "": - message.Type = model.MessageText - message.Data = updateMessage.Text - messages <- message case updateMessage.Contact.PhoneNumber != "": message.Type = model.MessageContact messages <- message case updateMessage.Location != nil: message.Type = model.MessageLocation messages <- message + case updateMessage.Text != "": + message.Type = model.MessageText + message.Data = updateMessage.Text + messages <- message } } } diff --git a/main_test.go b/main_test.go index 9f26155..9ab805e 100644 --- a/main_test.go +++ b/main_test.go @@ -6,8 +6,8 @@ import ( "os" "testing" - "github.com/eeonevision/tbot/internal/adapter" - "github.com/eeonevision/tbot/model" + "github.com/yanzay/tbot/internal/adapter" + "github.com/yanzay/tbot/model" ) func TestMain(m *testing.M) { @@ -69,7 +69,7 @@ func TestDocumentUpload(t *testing.T) { }) } requestResponse(t, setup, - "https://raw.githubusercontent.com/eeonevision/tbot/master/LICENSE", + "https://raw.githubusercontent.com/yanzay/tbot/master/LICENSE", model.MessageDocument, "OK", model.MessageText) } diff --git a/message.go b/message.go index e76b08b..021d47a 100644 --- a/message.go +++ b/message.go @@ -8,7 +8,7 @@ import ( "path/filepath" "strings" - "github.com/eeonevision/tbot/model" + "github.com/yanzay/tbot/model" ) // MessageVars is a parsed message variables lookup table diff --git a/message_processor.go b/message_processor.go index 92fc14e..18fb43f 100644 --- a/message_processor.go +++ b/message_processor.go @@ -3,8 +3,8 @@ package tbot import ( "strings" - "github.com/eeonevision/tbot/model" "github.com/yanzay/log" + "github.com/yanzay/tbot/model" ) func (s *Server) processMessage(message *Message) { diff --git a/message_test.go b/message_test.go index 4a71295..294c485 100644 --- a/message_test.go +++ b/message_test.go @@ -3,7 +3,7 @@ package tbot import ( "testing" - "github.com/eeonevision/tbot/model" + "github.com/yanzay/tbot/model" ) func TestReply(t *testing.T) { diff --git a/router_mux_test.go b/router_mux_test.go index b3ea9c4..85a662b 100644 --- a/router_mux_test.go +++ b/router_mux_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/eeonevision/tbot/model" + "github.com/yanzay/tbot/model" ) type testSequence struct { diff --git a/server.go b/server.go index 0c6349f..503b3d2 100644 --- a/server.go +++ b/server.go @@ -3,8 +3,8 @@ package tbot import ( "net/http" - "github.com/eeonevision/tbot/internal/adapter" - "github.com/eeonevision/tbot/model" + "github.com/yanzay/tbot/internal/adapter" + "github.com/yanzay/tbot/model" ) // Server is a telegram bot server. Looks and feels like net/http. From 36961c49b76dd99ec97f94932b3ec684cb077eb1 Mon Sep 17 00:00:00 2001 From: eeonevision Date: Mon, 29 Jan 2018 14:27:39 +0300 Subject: [PATCH 09/11] Fixed fails. --- internal/adapter/adapter.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/adapter/adapter.go b/internal/adapter/adapter.go index 4d473da..bd6eb9d 100644 --- a/internal/adapter/adapter.go +++ b/internal/adapter/adapter.go @@ -122,16 +122,16 @@ func (b *Bot) adaptUpdates(updates <-chan tgbotapi.Update, messages chan<- *mode continue } messages <- message + case updateMessage.Text != "": + message.Type = model.MessageText + message.Data = updateMessage.Text + messages <- message case updateMessage.Contact.PhoneNumber != "": message.Type = model.MessageContact messages <- message case updateMessage.Location != nil: message.Type = model.MessageLocation messages <- message - case updateMessage.Text != "": - message.Type = model.MessageText - message.Data = updateMessage.Text - messages <- message } } } From 3c14627bbbf3e341bce3c354cc04550c06e7b4a6 Mon Sep 17 00:00:00 2001 From: eeonevision Date: Sat, 3 Feb 2018 13:20:16 +0300 Subject: [PATCH 10/11] Much clear implementation of Location and Contact request buttons. --- internal/adapter/adapter.go | 54 ++++++++++++++++++++----------------- message.go | 26 +++++++++++++----- model/message.go | 13 +++------ 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/internal/adapter/adapter.go b/internal/adapter/adapter.go index bd6eb9d..5432279 100644 --- a/internal/adapter/adapter.go +++ b/internal/adapter/adapter.go @@ -114,6 +114,19 @@ func (b *Bot) adaptUpdates(updates <-chan tgbotapi.Update, messages chan<- *mode } } switch { + case updateMessage.Contact != nil: + message.Type = model.MessageContact + message.Data = fmt.Sprintf("%s - %s %s", + updateMessage.Contact.PhoneNumber, + updateMessage.Contact.FirstName, + updateMessage.Contact.LastName) + messages <- message + case updateMessage.Location != nil: + message.Type = model.MessageLocation + message.Data = fmt.Sprintf("%v|%v", + updateMessage.Location.Latitude, + updateMessage.Location.Longitude) + messages <- message case updateMessage.Document != nil: message.Type = model.MessageDocument message.Data, err = b.tbot.GetFileDirectURL(updateMessage.Document.FileID) @@ -126,12 +139,6 @@ func (b *Bot) adaptUpdates(updates <-chan tgbotapi.Update, messages chan<- *mode message.Type = model.MessageText message.Data = updateMessage.Text messages <- message - case updateMessage.Contact.PhoneNumber != "": - message.Type = model.MessageContact - messages <- message - case updateMessage.Location != nil: - message.Type = model.MessageLocation - messages <- message } } } @@ -175,10 +182,24 @@ func chattableFromMessage(m *model.Message) tgbotapi.Chattable { msg.ParseMode = tgbotapi.ModeMarkdown } return msg - case model.MessageSpecialKeyboard: + case model.MessageContactButton: msg := tgbotapi.NewMessage(m.ChatID, m.Data) - btns := buttonsFromSpecialButtons(m.SpecialButtons) - keyboard := tgbotapi.NewReplyKeyboard(btns...) + btn := [][]tgbotapi.KeyboardButton{ + []tgbotapi.KeyboardButton{ + tgbotapi.NewKeyboardButtonContact(m.ContactButton)}} + keyboard := tgbotapi.NewReplyKeyboard(btn...) + keyboard.OneTimeKeyboard = m.OneTimeKeyboard + msg.ReplyMarkup = keyboard + if m.Markdown { + msg.ParseMode = tgbotapi.ModeMarkdown + } + return msg + case model.MessageLocationButton: + msg := tgbotapi.NewMessage(m.ChatID, m.Data) + btn := [][]tgbotapi.KeyboardButton{ + []tgbotapi.KeyboardButton{ + tgbotapi.NewKeyboardButtonLocation(m.LocationButton)}} + keyboard := tgbotapi.NewReplyKeyboard(btn...) keyboard.OneTimeKeyboard = m.OneTimeKeyboard msg.ReplyMarkup = keyboard if m.Markdown { @@ -200,21 +221,6 @@ func buttonsFromStrings(strs [][]string) [][]tgbotapi.KeyboardButton { return btns } -func buttonsFromSpecialButtons(sbtns [][]model.KeyboardButton) [][]tgbotapi.KeyboardButton { - btns := make([][]tgbotapi.KeyboardButton, len(sbtns)) - for i, buttonRow := range sbtns { - btns[i] = make([]tgbotapi.KeyboardButton, len(buttonRow)) - for j, button := range buttonRow { - btns[i][j] = tgbotapi.KeyboardButton{ - Text: button.Text, - RequestContact: button.RequestContact, - RequestLocation: button.RequestLocation, - } - } - } - return btns -} - func fileMessage(m *model.Message, file tgbotapi.BaseFile) tgbotapi.BaseFile { if strings.HasPrefix(m.Data, "http") { _, err := url.Parse(m.Data) diff --git a/message.go b/message.go index 021d47a..7ff3b26 100644 --- a/message.go +++ b/message.go @@ -122,13 +122,27 @@ func (m *Message) ReplyKeyboard(text string, buttons [][]string, options ...Keyb m.sendReply(msg) } -// ReplySpecialKeyboard sends custom reply keyboard to the user with included special buttons. -func (m *Message) ReplySpecialKeyboard(text string, buttons [][]model.KeyboardButton, options ...KeyboardOption) { +// RequestContactButton sends custom reply contact button to the user. +func (m *Message) RequestContactButton(text string, button string, options ...KeyboardOption) { msg := &model.Message{ - Type: model.MessageSpecialKeyboard, - Data: text, - SpecialButtons: buttons, - ChatID: m.ChatID, + Type: model.MessageContactButton, + Data: text, + ContactButton: button, + ChatID: m.ChatID, + } + for _, option := range options { + option(msg) + } + m.sendReply(msg) +} + +// RequestLocationButton sends custom reply location keyboard to the user. +func (m *Message) RequestLocationButton(text string, button string, options ...KeyboardOption) { + msg := &model.Message{ + Type: model.MessageLocationButton, + Data: text, + ContactButton: button, + ChatID: m.ChatID, } for _, option := range options { option(msg) diff --git a/model/message.go b/model/message.go index 04fc0fb..b3263a5 100644 --- a/model/message.go +++ b/model/message.go @@ -11,7 +11,8 @@ const ( MessagePhoto MessageAudio MessageKeyboard - MessageSpecialKeyboard + MessageContactButton + MessageLocationButton ) const ( @@ -34,18 +35,12 @@ type Message struct { DisablePreview bool Markdown bool Buttons [][]string - SpecialButtons [][]KeyboardButton + ContactButton string + LocationButton string OneTimeKeyboard bool ForwardDate int } -// KeyboardButton is a button within a custom keyboard. -type KeyboardButton struct { - Text string `json:"text"` - RequestContact bool `json:"request_contact"` - RequestLocation bool `json:"request_location"` -} - // Contact contains information about a contact. // // Note that LastName and UserID may be empty. From cf16aaafba00beef6e82dd4b3f0eacaaf2db1a09 Mon Sep 17 00:00:00 2001 From: eeonevision Date: Sat, 3 Feb 2018 13:50:49 +0300 Subject: [PATCH 11/11] Fixed location button. --- message.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/message.go b/message.go index 7ff3b26..bff9975 100644 --- a/message.go +++ b/message.go @@ -139,10 +139,10 @@ func (m *Message) RequestContactButton(text string, button string, options ...Ke // RequestLocationButton sends custom reply location keyboard to the user. func (m *Message) RequestLocationButton(text string, button string, options ...KeyboardOption) { msg := &model.Message{ - Type: model.MessageLocationButton, - Data: text, - ContactButton: button, - ChatID: m.ChatID, + Type: model.MessageLocationButton, + Data: text, + LocationButton: button, + ChatID: m.ChatID, } for _, option := range options { option(msg)