Skip to content

Commit

Permalink
Merge pull request #5 from my1562/interactivity
Browse files Browse the repository at this point in the history
Interactivity
  • Loading branch information
corporateanon committed Apr 13, 2020
2 parents b04a643 + 190f394 commit 615cbaf
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 16 deletions.
35 changes: 25 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/my1562/telegrambot/pkg/apiclient"
"github.com/my1562/telegrambot/pkg/config"
mock_apiclient "github.com/my1562/telegrambot/pkg/mockApiclient"
"go.uber.org/dig"
)

Expand All @@ -22,12 +23,18 @@ func main() {
client := resty.New().SetHostURL(conf.APIURL)
return client
})
c.Provide(apiclient.New)
c.Provide(func(conf *config.Config, client *resty.Client) apiclient.IApiClient {
if conf.EmualteAPI {
return mock_apiclient.New()
} else {
return apiclient.New(client)
}
})

if err := c.Invoke(func(
bot *tgbotapi.BotAPI,
commandProcessor *CommandProcessor,
api *apiclient.ApiClient,
api apiclient.IApiClient,
) {
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
Expand All @@ -38,10 +45,6 @@ func main() {
log.Panic(err)
}

commandProcessor.Hears(`^hello`, func(ctx *CommandHandlerContext) {
fmt.Println(ctx.matches)
})

commandProcessor.Location(func(ctx *CommandHandlerContext) {
lat, lng := ctx.update.Message.Location.Latitude, ctx.update.Message.Location.Longitude
geocodingResult, err := api.Geocode(lat, lng, 300)
Expand All @@ -60,24 +63,36 @@ func main() {
)
}

msg := tgbotapi.NewMessage(ctx.chatID, "Оберіть свою адресу зі списку")
msg := tgbotapi.NewMessage(ctx.chatID, "Выберите свой адрес из списка")
msg.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(results...)

if _, err := bot.Send(msg); err != nil {
log.Panic(err)
}
})

commandProcessor.Callback(`subAddr:(\d+)`, func(ctx *CommandHandlerContext) {
fmt.Println(ctx.matches)

addressIDAr, err := strconv.ParseInt(ctx.matches[1], 10, 64)
if err != nil {
panic(err)
log.Panic(err)
}
log.Printf("Subscribe to: addressID:%d, chatID:%d", addressIDAr, ctx.chatID)

err = api.CreateSubscription(ctx.chatID, addressIDAr)
if err != nil {
panic(err)
log.Panic(err)
}

addressString, err := api.AddressStringByID(addressIDAr)
if err != nil {
log.Panic(err)
}

msgText := "Вы подписались на обновления для адреса:\n" + addressString
msg := tgbotapi.NewMessage(ctx.chatID, msgText)
if _, err := bot.Send(msg); err != nil {
log.Panic(err)
}
})

Expand Down
45 changes: 43 additions & 2 deletions pkg/apiclient/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ import (
"github.com/go-resty/resty/v2"
)

type IApiClient interface {
CreateSubscription(chatID int64, addressArID int64) error
Geocode(
lat float64,
lng float64,
accuracy float64,
) (*GeocodeResponse, error)
AddressStringByID(ID int64) (string, error)
}

type ApiClient struct {
client *resty.Client
}

func New(client *resty.Client) *ApiClient {
func New(client *resty.Client) IApiClient {
return &ApiClient{client: client}
}

Expand All @@ -37,7 +47,6 @@ func (api *ApiClient) CreateSubscription(chatID int64, addressArID int64) error
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -80,3 +89,35 @@ func (api *ApiClient) Geocode(
result := resp.Result()
return result.(*Response).Result, nil
}

type AddressLookupResponse struct {
Address struct {
Address string
}
}

func (api *ApiClient) AddressStringByID(
ID int64,
) (string, error) {
type Response struct {
Result *AddressLookupResponse
}

resp, err := api.client.R().
SetResult(&Response{}).
SetError(&ErrorResponse{}).
SetPathParams(
map[string]string{
"id": strconv.FormatInt(ID, 10),
},
).
Get("/address/geocode/lookup/{id}")
if err != nil {
return "", err
}
if backendError := resp.Error(); backendError != nil {
return "", backendError.(*ErrorResponse)
}
result := resp.Result()
return result.(*Response).Result.Address.Address, nil
}
10 changes: 6 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

type Config struct {
TGToken string
APIURL string
TGToken string
APIURL string
EmualteAPI bool
}

func NewConfig() (*Config, error) {
Expand All @@ -25,7 +26,8 @@ func NewConfig() (*Config, error) {
fmt.Println(err)
}
return &Config{
TGToken: os.Getenv("TELEGRAM_APITOKEN"),
APIURL: os.Getenv("API_URL"),
TGToken: os.Getenv("TELEGRAM_APITOKEN"),
APIURL: os.Getenv("API_URL"),
EmualteAPI: os.Getenv("EMULATE_API") == "true",
}, nil
}
31 changes: 31 additions & 0 deletions pkg/mockApiclient/mockApiclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mock_apiclient

import "github.com/my1562/telegrambot/pkg/apiclient"

type MockApiClient struct{}

func New() apiclient.IApiClient {
return &MockApiClient{}
}

func (api *MockApiClient) CreateSubscription(chatID int64, addressArID int64) error {
return nil
}
func (api *MockApiClient) Geocode(
lat float64,
lng float64,
accuracy float64,
) (*apiclient.GeocodeResponse, error) {
return &apiclient.GeocodeResponse{
Addresses: []apiclient.GeocodeResponseAddress{
{5, 10, "221b Baker st."},
{6, 12, "1 Lenin st."},
},
}, nil
}

func (api *MockApiClient) AddressStringByID(
ID int64,
) (string, error) {
return "221b Baker st.", nil
}

0 comments on commit 615cbaf

Please sign in to comment.