Skip to content

Commit

Permalink
feat: send message to user
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Jun 20, 2021
1 parent e0c7f85 commit b4be6c1
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
16 changes: 15 additions & 1 deletion api/notify/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@ package handler
import (
"fmt"
"net/http"
"strconv"

"github.com/indes/telepush/internal/bot"
)

func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello telepush")
fmt.Fprintf(w, "hello telepush\n")

params := r.URL.Query()
userId, _ := strconv.Atoi(params.Get("u"))

if err := bot.NotifyTxtMessage(userId, "hello, I am telepush!"); err != nil {
fmt.Fprintf(w, "send message to %d failed!", userId)
return
}

fmt.Fprintf(w, "send message to %d succes!", userId)

}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/indes/telepush

go 1.16

require (
go.uber.org/atomic v1.8.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.17.0 // indirect
gopkg.in/tucnak/telebot.v2 v2.3.5
)
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
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/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.8.0 h1:CUhrE4N1rqSE6FM9ecihEjRkLQu8cDfgDyoOs83mEY4=
go.uber.org/atomic v1.8.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/tucnak/telebot.v2 v2.3.5 h1:TdMJTlG8kvepsvZdy/gPeYEBdwKdwFFjH1AQTua9BOU=
gopkg.in/tucnak/telebot.v2 v2.3.5/go.mod h1:BgaIIx50PSRS9pG59JH+geT82cfvoJU/IaI5TJdN3v8=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
57 changes: 56 additions & 1 deletion internal/bot/bot.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
package bot

func NewBot() {
import (
"errors"

"github.com/indes/telepush/internal/config"
"go.uber.org/zap"
tb "gopkg.in/tucnak/telebot.v2"
)

func NewBot() (*tb.Bot, error) {
bot, err := tb.NewBot(tb.Settings{
Token: config.BotToken,
})

if err != nil {
zap.S().Errorf("NewBot failed, err %v", err)
return nil, err
}

return bot, nil
}

func Notify(reciverId int64, msg *tb.Message) error {

bot, err := NewBot()
if err != nil {
return errors.New("Get telegram bot failed")
}

reciver := &tb.User{
ID: int(reciverId),
}

if _, err := bot.Send(reciver, msg); err != nil {
zap.S().Errorf("bot send message failed, to: %v msg: %v err: %v", reciverId, msg, err)
return errors.New("Telegram bot send message failed,")
}

return nil
}

func NotifyTxtMessage(reciverId int, msg string) error {

bot, err := NewBot()
if err != nil {
return errors.New("Get telegram bot failed")
}

reciver := &tb.User{
ID: reciverId,
}

if _, err := bot.Send(reciver, msg); err != nil {
zap.S().Errorf("bot send message failed, to: %v msg: %v err: %v", reciverId, msg, err)
return errors.New("Telegram bot send message failed,")
}

return nil
}
7 changes: 7 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package config

import "os"

var (
BotToken = os.Getenv("telegram_bot_token")
)
File renamed without changes.

0 comments on commit b4be6c1

Please sign in to comment.