Skip to content

Commit

Permalink
Priority Checker
Browse files Browse the repository at this point in the history
  • Loading branch information
no committed May 7, 2020
1 parent 767aede commit 260a078
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 15 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ go 1.13
require (
github.com/go-resty/resty/v2 v2.2.0
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/gorilla/mux v1.7.4 // indirect
github.com/hibiken/asynq v0.8.2
github.com/joho/godotenv v1.3.0
github.com/my1562/queue v0.0.3
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
go.uber.org/dig v1.8.0
go.uber.org/dig v1.9.0
)
48 changes: 45 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-redis/redis/v7 v7.2.0 h1:CrCexy/jYWZjW0AyVoHlcJUeZN19VWlbepTh1Vq6dJs=
github.com/go-redis/redis/v7 v7.2.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
github.com/go-resty/resty/v2 v2.2.0 h1:vgZ1cdblp8Aw4jZj3ZsKh6yKAlMg3CHMrqFSFFd+jgY=
github.com/go-resty/resty/v2 v2.2.0/go.mod h1:nYW/8rxqQCmI3bPz9Fsmjbr2FBjGuR2Mzt6kDh3zZ7w=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hibiken/asynq v0.8.1/go.mod h1:T2LLFy2NDRH0yniH/62wKygCrAKuaO4dHyOFYGmSdJY=
github.com/hibiken/asynq v0.8.2 h1:U8EOe6t9xhzDPLw0TyCQKGHCmGvRGhjYWqyCMbtmv/c=
github.com/hibiken/asynq v0.8.2/go.mod h1:T2LLFy2NDRH0yniH/62wKygCrAKuaO4dHyOFYGmSdJY=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/my1562/queue v0.0.3 h1:tt9bvkjyJGSrUh0vznDFdBHgiv/0/bDJh9Oug4yDCCE=
github.com/my1562/queue v0.0.3/go.mod h1:lDreWq9koc5saiqeutYcYsO5weZZp9iy9EGn0pvFW+Y=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
go.uber.org/dig v1.8.0 h1:1rR6hnL/bu1EVcjnRDN5kx1vbIjEJDTGhSQ2B3ddpcI=
go.uber.org/dig v1.8.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
go.uber.org/dig v1.9.0 h1:pJTDXKEhRqBI8W7rU7kwT5EgyRZuSMVSFcZolOvKK9U=
go.uber.org/dig v1.9.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0 h1:MsuvTghUPjX762sGLnGsxC3HM0B5r83wEtYcYR8/vRs=
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e h1:9vRrk9YW2BTzLP0VCB9ZDjU4cPqkg+IDWL7XgxA1yxQ=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
48 changes: 45 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,32 @@ import (
"github.com/my1562/telegrambot/pkg/apiclient"
"github.com/my1562/telegrambot/pkg/config"
mock_apiclient "github.com/my1562/telegrambot/pkg/mockApiclient"
"github.com/my1562/telegrambot/pkg/priorityChecker"
"go.uber.org/dig"
)

func getAddressStatusMessage(
message string,
addressString string,
addressStatus apiclient.AddressArCheckStatus,
) string {

introduction := ""
emojiIcon := ""

if addressStatus == apiclient.AddressStatusNoWork {
introduction = "Работы не проводятся"
emojiIcon = "✅"
}
if addressStatus == apiclient.AddressStatusWork {
introduction = ""
emojiIcon = "🛠"
}

fullMessageText := emojiIcon + " " + addressString + ": " + introduction + "\n\n" + message
return fullMessageText
}

func main() {

c := dig.New()
Expand All @@ -26,16 +49,18 @@ func main() {
c.Provide(func(conf *config.Config, client *resty.Client) apiclient.IApiClient {
if conf.EmualteAPI {
return mock_apiclient.New()
} else {
return apiclient.New(client)
}
return apiclient.New(client)
})
c.Provide(priorityChecker.NewPriorityChecker)

if err := c.Invoke(func(
bot *tgbotapi.BotAPI,
commandProcessor *CommandProcessor,
api apiclient.IApiClient,
priorityChecker *priorityChecker.PriorityChecker,
) {

log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
Expand Down Expand Up @@ -89,11 +114,28 @@ func main() {
log.Panic(err)
}

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

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

log.Printf("Enqueueing priority check for address (ID=%d): %s\n", addressIDAr, addressString)
if err := priorityChecker.EnqueuePriorityCheck(addressIDAr); err != nil {
log.Panic(err)
}
})

for update := range updates {
Expand Down
55 changes: 50 additions & 5 deletions pkg/apiclient/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ package apiclient

import (
"strconv"
"time"

"github.com/go-resty/resty/v2"
)

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

type ApiClient struct {
Expand Down Expand Up @@ -121,3 +119,50 @@ func (api *ApiClient) AddressStringByID(
result := resp.Result()
return result.(*Response).Result.Address.Address, nil
}

type AddressArCheckStatus string

const (
AddressStatusNoWork AddressArCheckStatus = "nowork"
AddressStatusWork = "work"
AddressStatusInit = "init"
)

type AddressByIDResponse struct {
ID int64
CheckStatus AddressArCheckStatus
ServiceMessage string
Hash string
TakenAt time.Time
CheckedAt time.Time
}

func (api *ApiClient) AddressByID(
ID int64,
) (*AddressByIDResponse, error) {
type Response struct {
Result *AddressByIDResponse
}

resp, err := api.client.R().
SetResult(&Response{}).
SetError(&ErrorResponse{}).
SetPathParams(
map[string]string{
"id": strconv.FormatInt(ID, 10),
},
).
Get("/address/{id}")
if err != nil {
return nil, err
}
if backendError := resp.Error(); backendError != nil {
//Normal case - address not yet exists
if resp.StatusCode() == 404 {
return nil, nil
}
return nil, backendError.(*ErrorResponse)
}
result := resp.Result()
return result.(*Response).Result, nil
}
11 changes: 9 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type Config struct {
TGToken string
APIURL string
Redis string
EmualteAPI bool
}

Expand All @@ -25,9 +26,15 @@ func NewConfig() (*Config, error) {
if err != nil {
fmt.Println(err)
}
return &Config{
config := &Config{
TGToken: os.Getenv("TELEGRAM_APITOKEN"),
APIURL: os.Getenv("API_URL"),
EmualteAPI: os.Getenv("EMULATE_API") == "true",
}, nil
Redis: os.Getenv("REDIS"),
}
if config.Redis == "" {
config.Redis = "127.0.0.1:6379"
}

return config, nil
}
6 changes: 6 additions & 0 deletions pkg/mockApiclient/mockApiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ func (api *MockApiClient) AddressStringByID(
) (string, error) {
return "221b Baker st.", nil
}

func (api *MockApiClient) AddressByID(
ID int64,
) (*apiclient.AddressByIDResponse, error) {
return nil, nil
}
27 changes: 27 additions & 0 deletions pkg/priorityChecker/priorityChecker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package priorityChecker

import (
"github.com/hibiken/asynq"
"github.com/my1562/queue"
"github.com/my1562/telegrambot/pkg/config"
)

type PriorityChecker struct {
client *asynq.Client
}

func NewPriorityChecker(config *config.Config) *PriorityChecker {
redis := asynq.RedisClientOpt{Addr: config.Redis}
client := asynq.NewClient(redis)

return &PriorityChecker{
client: client,
}
}

func (p *PriorityChecker) EnqueuePriorityCheck(addrID int64) error {
if err := p.client.Enqueue(queue.NewPriorityCheckTask(addrID)); err != nil {
return err
}
return nil
}

0 comments on commit 260a078

Please sign in to comment.