Skip to content

Commit

Permalink
feat: Enhance Telegram bot message formatting with Markdown support a…
Browse files Browse the repository at this point in the history
…nd improve user instructions
  • Loading branch information
Laisky committed Nov 17, 2024
1 parent e4d13f8 commit 0217f7c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 63 deletions.
42 changes: 27 additions & 15 deletions internal/web/telegram/service/arweave_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ func (s *Telegram) registerArweaveAliasHandler() {
lastT: gutils.Clock.GetUTCNow(),
})

if _, err := s.bot.Send(m.Sender, gutils.Dedent(`
Reply number:
1 - create alias # reply "1 - <ALIAS_NAME> arweave_file_id"
2 - update alias # reply "2 - <ALIAS_NAME> arweave_file_id"
3 - get alias # reply "3 - <ALIAS_NAME>"
<ALIAS_NAME> must match ^[a-zA-Z0-9_\-\.]{3,64}$
For more info, check this doc: https://ario.laisky.com/alias/doc
Check all DNS records at this site(up to 1000 records, refresh every 10 minutes): https://ario.laisky.com/dns
`)); err != nil {
if _, err := s.bot.Send(m.Sender,
"Reply number:\n\n"+
"1. create alias # reply `1 - <ALIAS_NAME> <ARWEAVE_FILE_ID>`\n"+
"2. update alias # reply `2 - <ALIAS_NAME> <ARWEAVE_FILE_ID>`\n"+
"3. get alias # reply `3 - <ALIAS_NAME>`\n\n"+
"`<ALIAS_NAME>` must match `^[a-zA-Z0-9_\\-\\.]{3,64}$`, "+
"For more info, [check this doc](https://ario.laisky.com/alias/doc). "+
"Check all DNS records [at this site](https://ario.laisky.com/dns)(up to 1000 records, refresh every 10 minutes).",
&tb.SendOptions{
ParseMode: tb.ModeMarkdown,
DisableWebPagePreview: true,
},
); err != nil {
return errors.Wrap(err, "send msg")
}

Expand Down Expand Up @@ -173,7 +173,13 @@ func (s *Telegram) arweaveCreateAlias(ctx context.Context, us *userStat, msg str
return errors.Errorf("request failed: [%d]%s", resp.StatusCode, string(cnt))
}

if _, err = s.bot.Send(us.user, "https://ario.laisky.com/alias/"+alias); err != nil {
if _, err = s.bot.Send(us.user,
fmt.Sprintf("https://ario.laisky.com/alias/%s", alias),
&tb.SendOptions{
ParseMode: tb.ModeMarkdown,
DisableWebPagePreview: true,
},
); err != nil {
return errors.Wrap(err, "send msg")
}

Expand Down Expand Up @@ -239,7 +245,13 @@ func (s *Telegram) arweaveUpdateAlias(ctx context.Context, us *userStat, msg str
return errors.Errorf("request failed: [%d]%s", resp.StatusCode, string(cnt))
}

if _, err = s.bot.Send(us.user, "https://ario.laisky.com/alias/"+alias); err != nil {
if _, err = s.bot.Send(us.user,
fmt.Sprintf("https://ario.laisky.com/alias/%s", alias),
&tb.SendOptions{
ParseMode: tb.ModeMarkdown,
DisableWebPagePreview: true,
},
); err != nil {
return errors.Wrap(err, "send msg")
}

Expand Down
71 changes: 35 additions & 36 deletions internal/web/telegram/service/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ func (s *Telegram) registerMonitorHandler() {
lastT: gutils.Clock.GetUTCNow(),
})

if _, err := s.bot.Send(m.Sender, gutils.Dedent(`
Reply number:
1 - new alert's name # reply "1 - alert_name"
2 - list all joint alerts # reply "2"
3 - join alert # reply "3 - alert_name:join_key"
4 - refresh push_token & join_key # reply "4 - alert_name"
5 - quit alert # reply "5 - alert_name"
6 - kick user # reply "6 - alert_name:uid"
For more info, check this doc: https://github.com/Laisky/laisky-blog-graphql/tree/master/internal/web/telegram
`)); err != nil {
if _, err := s.bot.Send(m.Sender,
"Reply number:\n\n"+
"1. new alert's name # reply `1 - <ALERT_NAME>`\n"+
"2. list all joint alerts # reply `2`\n"+
"3. join alert # reply `3 - <ALERT_NAME>:<JOIN_KEY>`\n"+
"4. refresh push token & `join_key` # reply `4 - <ALERT_NAME>`\n"+
"5. quit alert # reply `5 - <ALERT_NAME>`\n"+
"6. kick user # reply `6 - <ALERT_NAME>:<UID>`\n\n"+
"For more info, [check this doc](https://github.com/Laisky/laisky-blog-graphql/tree/master/internal/web/telegram).",
&tb.SendOptions{
ParseMode: tb.ModeMarkdown,
DisableWebPagePreview: true,
},
); err != nil {
return errors.Wrap(err, "send msg")
}

Expand Down Expand Up @@ -134,16 +136,16 @@ func (s *Telegram) kickUser(ctx context.Context, us *userStat, au string) (err e
}

if err = s.monitorDao.RemoveUAR(ctx, kickedUser.UID, alertName); err != nil {
return errors.Wrap(err, "remove user_alert_relation")
return errors.Wrap(err, "remove user alert relation")
}
log.Logger.Info("remove user_alert_relation",
zap.String("user_name", kickedUser.Name),
zap.String("alert_type", alertName),
zap.String("user", kickedUser.ID.Hex()))

msg := "<" + us.user.Username + "> kick user:\n"
msg += "alert_type: " + alertName + "\n"
msg += "kicked_user: " + kickedUser.Name + " (" + ans[1] + ")\n"
msg := "<`" + us.user.Username + "`> kick user:\n" +
"alert type: `" + alertName + "`\n" +
"kicked user: `" + kickedUser.Name + "` (`" + ans[1] + "`)\n"

users, err := s.monitorDao.LoadUsersByAlertType(ctx, alertType)
if err != nil {
Expand All @@ -167,7 +169,7 @@ func (s *Telegram) kickUser(ctx context.Context, us *userStat, au string) (err e
func (s *Telegram) userQuitAlert(ctx context.Context,
us *userStat, alertName string) (err error) {
if err = s.monitorDao.RemoveUAR(ctx, int(us.user.ID), alertName); err != nil {
return errors.Wrap(err, "remove user_alert_relation by uid and alert_name")
return errors.Wrap(err, "remove user alert relation by uid and alert_name")
}

return s.SendMsgToUser(int(us.user.ID), "successed unsubscribe "+alertName)
Expand All @@ -183,10 +185,10 @@ func (s *Telegram) refreshAlertTokenAndKey(ctx context.Context, us *userStat, al
return errors.Wrap(err, "refresh alert token and key")
}

msg := "<" + us.user.Username + "> refresh token:\n"
msg += "alert_type: " + alertType.Name + "\n"
msg += "push_token: " + alertType.PushToken + "\n"
msg += "join_key: " + alertType.JoinKey + "\n"
msg := "<`" + us.user.Username + "`> refresh token:\n"
msg += "alert type: `" + alertType.Name + "`\n"
msg += "push token: `" + alertType.PushToken + "`\n"
msg += "join key: `" + alertType.JoinKey + "`\n"

users, err := s.monitorDao.LoadUsersByAlertType(ctx, alertType)
if err != nil {
Expand Down Expand Up @@ -270,9 +272,9 @@ func (s *Telegram) listAllMonitorAlerts(ctx context.Context,
msg = ""
for _, alert := range alerts {
msg += "--------------------------------\n"
msg += "alert_type: " + alert.Name + "\n"
msg += "push_token: " + alert.PushToken + "\n"
msg += "join_key: " + alert.JoinKey + "\n"
msg += "alert type: `" + alert.Name + "`\n"
msg += "push token: `" + alert.PushToken + "`\n"
msg += "join key: `" + alert.JoinKey + "`\n"
}
msg += "--------------------------------"
}
Expand All @@ -288,24 +290,21 @@ func (s *Telegram) createNewMonitor(ctx context.Context, us *userStat, alertName

a, err := s.monitorDao.CreateAlertType(ctx, alertName)
if err != nil {
return errors.Wrap(err, "create alert_type")
return errors.Wrap(err, "create alert type")
}

_, err = s.monitorDao.CreateOrGetUserAlertRelations(ctx, u, a)
if err != nil {
return errors.Wrap(err, "create user_alert_relation")
return errors.Wrap(err, "create user alert relation")
}

if _, err = s.bot.Send(us.user, fmt.Sprintf(`
create user & alert_type & user_alert_relations successed!
user: %v
alert_type: %v
join_key: %v
push_token: %v
`, u.Name,
a.Name,
a.JoinKey,
a.PushToken)); err != nil {
if _, err = s.bot.Send(us.user,
"create user & alert type & user alert relations successed!\n"+
fmt.Sprintf("user: `%v`\n", u.Name)+
fmt.Sprintf("alert type: `%v`\n", a.Name)+
fmt.Sprintf("join key: `%v`\n", a.JoinKey)+
fmt.Sprintf("push token: `%v`\n", a.PushToken),
); err != nil {
return errors.Wrap(err, "send msg")
}

Expand Down
26 changes: 15 additions & 11 deletions internal/web/telegram/service/notes_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ func (s *Telegram) registerNotesSearchHandler() {
lastT: gutils.Clock.GetUTCNow(),
})

if _, err := s.bot.Send(m.Sender, gutils.Dedent(`
Reply keyword to search notes, do not contain any blank space, regex is supported.
For more info, check this doc: https://t.me/laiskynotes/298
`)); err != nil {
if _, err := s.bot.Send(m.Sender,
"Reply keyword to search notes, do not contain any blank space, regex is supported.\n"+
"For more info, check [this doc](https://t.me/laiskynotes/298).",
&tb.SendOptions{
ParseMode: tb.ModeMarkdown,
DisableWebPagePreview: true,
},
); err != nil {
return errors.Wrap(err, "send msg")
}

Expand Down Expand Up @@ -80,16 +83,17 @@ func (s *Telegram) notesSearchByKeyword(ctx context.Context, us *userStat, msg s
summary = string([]rune(summary)[:noteSummaryLen]) + "..."
}

resp += fmt.Sprintf(gutils.Dedent(`
-------------------------------------
url: https://t.me/laiskynotes/%d
content: %s`), note.PostID, summary)
resp += "\n"
resp = "-------------------------------------\n" +
fmt.Sprintf("link: https://t.me/laiskynotes/%d\n", note.PostID) +
fmt.Sprintf("note: %s\n", summary)
}

resp += "-------------------------------------"

if _, err = s.bot.Send(us.user, resp); err != nil {
if _, err = s.bot.Send(us.user, resp, &tb.SendOptions{
ParseMode: tb.ModeMarkdown,
DisableWebPagePreview: true,
}); err != nil {
return errors.Wrap(err, "send msg")
}

Expand Down
7 changes: 6 additions & 1 deletion internal/web/telegram/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ func (s *Telegram) PleaseRetry(sender *tb.User, msg string) {
}

func (s *Telegram) SendMsgToUser(uid int, msg string) (err error) {
_, err = s.bot.Send(&tb.User{ID: int64(uid)}, msg)
_, err = s.bot.Send(&tb.User{ID: int64(uid)}, msg,
&tb.SendOptions{
ParseMode: tb.ModeMarkdown,
DisableWebPagePreview: true,
},
)
return err
}

0 comments on commit 0217f7c

Please sign in to comment.