Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
Fix telegram user id conflict in reply

Fix nil pointer dereference
  • Loading branch information
web-flow committed Jan 24, 2021
1 parent 79feb19 commit f5036f8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 45 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.5.6] - 2021-01-24

### Changed
- Now available to access the archive.today's tor service if enable service of archive.today.
- Publish multiple arch snapcraft app.
- Refine workflows.

### Fixed
- Fix telegram user id conflict in reply.
- Fix nil pointer dereference.

## [0.5.5] - 2021-01-15

### Added
Expand Down
2 changes: 1 addition & 1 deletion cmd/wayback/wayback.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func output(tit string, args map[string]string) {
func archive(cmd *cobra.Command, opts *config.Options, args []string) {
archiving := func(ctx context.Context, urls []string) error {
g, ctx := errgroup.WithContext(ctx)
var wbrc wayback.Broker = &wayback.Handle{URLs: urls}
var wbrc wayback.Broker = &wayback.Handle{URLs: urls, Opts: opts}

for slot, do := range opts.Slots() {
slot, do := slot, do
Expand Down
91 changes: 47 additions & 44 deletions service/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import (
"github.com/wabarc/wayback/logger"
"github.com/wabarc/wayback/publish"
"github.com/wabarc/wayback/utils"
"golang.org/x/sync/errgroup"
)

type telegram struct {
opts *config.Options

bot *tgbotapi.BotAPI
upd tgbotapi.Update
}

// New telegram struct.
Expand All @@ -32,71 +34,72 @@ func New(opts *config.Options) *telegram {

// Serve loop request message from the Telegram api server.
// Serve always returns a nil error.
func (t *telegram) Serve(_ context.Context) error {
bot, err := tgbotapi.NewBotAPI(t.opts.TelegramToken())
if err != nil {
func (t *telegram) Serve(_ context.Context) (err error) {
if t.bot, err = tgbotapi.NewBotAPI(t.opts.TelegramToken()); err != nil {
return errors.New("Initialize telegram failed, error: %v", err)
}

logger.Info("Telegram: authorized on account %s", bot.Self.UserName)
logger.Info("Telegram: authorized on account %s", t.bot.Self.UserName)

u := tgbotapi.NewUpdate(0)
u.Timeout = 600
u.Timeout = 60

updates, err := bot.GetUpdatesChan(u)
updates, err := t.bot.GetUpdatesChan(u)
if err != nil {
return errors.New("Get telegram message channel failed, error: %v", err)
}

g, _ := errgroup.WithContext(context.Background())
for update := range updates {
if update.Message == nil { // ignore any non-Message Updates
continue
}

message := update.Message
text := message.Text
logger.Debug("Telegram: message: %s", text)
t.upd = update
go t.process()
}

urls := utils.MatchURL(text)
switch {
case message.IsCommand():
continue
case len(urls) == 0:
logger.Info("Telegram: archives failure, URL no found.")
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "URL no found.")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
continue
}
return nil
}

g.Go(func() error {
col, msgID, err := archive(t, update.Message.MessageID, urls)
if err != nil {
logger.Error("Telegram: archiving failed, ", err)
return err
}
func (t *telegram) process() {
bot, update := t.bot, t.upd
message := update.Message
text := message.Text
logger.Debug("Telegram: message: %s", text)

urls := utils.MatchURL(text)
switch {
case message.IsCommand():
return
case len(urls) == 0:
logger.Info("Telegram: archives failure, URL no found.")
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "URL no found.")
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
return
}

col, err := t.archive(urls)
if err != nil {
logger.Error("Telegram: archiving failed, ", err)
return
}

replyText := publish.Render(col)
logger.Debug("Telegram: reply text, %s", replyText)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, replyText)
msg.ReplyToMessageID = msgID
msg.ParseMode = "html"
replyText := publish.Render(col)
logger.Debug("Telegram: reply text, %s", replyText)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, replyText)
msg.ReplyToMessageID = update.Message.MessageID
msg.ParseMode = "html"

bot.Send(msg)
bot.Send(msg)

if t.opts.TelegramChannel() != "" {
logger.Debug("Telegram: publishing to channel...")
publish.ToChannel(t.opts, bot, replyText)
}
return nil
})
if t.opts.TelegramChannel() != "" {
logger.Debug("Telegram: publishing to channel...")
publish.ToChannel(t.opts, bot, replyText)
}

return nil
}

func archive(t *telegram, msgid int, urls []string) (col []*publish.Collect, id int, err error) {
func (t *telegram) archive(urls []string) (col []*publish.Collect, err error) {
logger.Debug("Telegram: archives start...")

wg := sync.WaitGroup{}
Expand Down Expand Up @@ -128,5 +131,5 @@ func archive(t *telegram, msgid int, urls []string) (col []*publish.Collect, id
}
wg.Wait()

return col, msgid, nil
return col, nil
}

0 comments on commit f5036f8

Please sign in to comment.