Skip to content

Commit

Permalink
Attach a button below the message for send a wayback request
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow committed Apr 23, 2021
1 parent 22482ec commit db8d961
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
74 changes: 54 additions & 20 deletions service/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"

telegram "github.com/go-telegram-bot-api/telegram-bot-api/v5"
Expand All @@ -22,6 +23,7 @@ import (

type Telegram struct {
bot *telegram.BotAPI
pub *publish.Telegram
}

// New Telegram struct.
Expand All @@ -36,6 +38,7 @@ func New() *Telegram {

return &Telegram{
bot: bot,
pub: publish.NewTelegram(bot),
}
}

Expand All @@ -60,12 +63,20 @@ func (t *Telegram) Serve(ctx context.Context) (err error) {
}()

for update := range updates {
if update.Message == nil { // ignore any non-Message Updates
if update.Message != nil {
logger.Debug("[telegram] message: %v", update.Message)
go t.process(ctx, update)
continue
}
logger.Debug("[telegram] message: %v", update.Message)

go t.process(ctx, update)
if update.CallbackQuery != nil {
logger.Debug("[telegram] callback query: %#v", update.CallbackQuery)
callback := update.CallbackQuery
if strings.HasPrefix(callback.Data, callbackPrefix()) {
go t.archive(ctx, callback.Message, helper.MatchURL(callback.Data))
}
continue
}
logger.Debug("[telegram] message empty, update: %#v", update)
}

return errors.New("done")
Expand All @@ -81,7 +92,6 @@ func (t *Telegram) process(ctx context.Context, update telegram.Update) error {
content = fmt.Sprintf("Text: \n%s\nCaption: \n%s", content, message.Caption)
}
urls := helper.MatchURL(content)
tel := publish.NewTelegram(t.bot)

// Set command as playback if receive a playback command without URLs, and
// required user reply a message with URLs.
Expand All @@ -99,20 +109,7 @@ func (t *Telegram) process(ctx context.Context, update telegram.Update) error {
t.bot.Send(msg)
return nil
case command == "playback", command == "search":
if len(urls) == 0 {
msg := telegram.NewMessage(message.Chat.ID, "Please send me URLs to playback...")
msg.ReplyToMessageID = message.MessageID
msg.BaseChat.ReplyMarkup = telegram.ForceReply{ForceReply: true}
t.bot.Send(msg)
return nil
}
t.bot.Send(telegram.NewChatAction(message.Chat.ID, telegram.ChatTyping))
col, _ := wayback.Playback(urls)
msg := telegram.NewMessage(message.Chat.ID, tel.Render(col))
msg.ReplyToMessageID = message.MessageID
msg.ParseMode = "html"
t.bot.Send(msg)
return nil
return t.playback(message, urls)
case message.IsCommand():
commands := t.myCommands()
if commands != "" {
Expand All @@ -130,6 +127,10 @@ func (t *Telegram) process(ctx context.Context, update telegram.Update) error {
return nil
}

return t.archive(ctx, message, urls)
}

func (t *Telegram) archive(ctx context.Context, message *telegram.Message, urls []string) error {
msg := telegram.NewMessage(message.Chat.ID, "Archiving...")
msg.ReplyToMessageID = message.MessageID
stage, err := t.bot.Send(msg)
Expand All @@ -146,9 +147,10 @@ func (t *Telegram) process(ctx context.Context, update telegram.Update) error {
return err
}

replyText := tel.Render(col)
replyText := t.pub.Render(col)
logger.Debug("[telegram] reply text, %s", replyText)
updMsg := telegram.NewEditMessageText(stage.Chat.ID, stage.MessageID, replyText)
updMsg.DisableWebPagePreview = true
updMsg.ParseMode = "html"
if _, err := t.bot.Send(updMsg); err != nil {
logger.Error("[telegram] update message failed: %v", err)
Expand All @@ -161,6 +163,34 @@ func (t *Telegram) process(ctx context.Context, update telegram.Update) error {
return nil
}

func (t *Telegram) playback(message *telegram.Message, urls []string) error {
if len(urls) == 0 {
msg := telegram.NewMessage(message.Chat.ID, "Please send me URLs to playback...")
msg.ReplyToMessageID = message.MessageID
msg.BaseChat.ReplyMarkup = telegram.ForceReply{ForceReply: true}
if _, err := t.bot.Send(msg); err != nil {
return err
}
return nil
}

t.bot.Send(telegram.NewChatAction(message.Chat.ID, telegram.ChatTyping))
col, _ := wayback.Playback(urls)

msg := telegram.NewMessage(message.Chat.ID, t.pub.Render(col))
msg.ReplyToMessageID = message.MessageID
// Attach a button below the message to send a wayback request quickly
msg.BaseChat.ReplyMarkup = telegram.NewInlineKeyboardMarkup(telegram.NewInlineKeyboardRow(
telegram.NewInlineKeyboardButtonData("wayback", callbackPrefix()+message.Text),
))
msg.DisableWebPagePreview = true
msg.ParseMode = "html"
if _, err := t.bot.Send(msg); err != nil {
return err
}
return nil
}

func (t *Telegram) myCommands() string {
commands, err := t.bot.GetMyCommands()
if err != nil {
Expand All @@ -174,3 +204,7 @@ func (t *Telegram) myCommands() string {

return list
}

func callbackPrefix() string {
return ":wayback "
}
2 changes: 1 addition & 1 deletion service/telegram/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestProcess(t *testing.T) {
if err := tg.process(context.Background(), update); err != nil {
t.Fatalf("process telegram message failed: %v", err)
} else {
time.Sleep(time.Second)
time.Sleep(3 * time.Second)
break
}
}
Expand Down

0 comments on commit db8d961

Please sign in to comment.