Resolve monitoring PF bot deep links
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 35s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 35s
This commit is contained in:
@@ -66,9 +66,15 @@ func (s Server) accessMe(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeError(w, http.StatusInternalServerError, err.Error())
|
writeError(w, http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
botUsername := s.App.Cfg.TGBotUsername
|
||||||
|
if botUsername == "" {
|
||||||
|
if resolved, err := s.App.TG.BotUsername(r.Context()); err == nil {
|
||||||
|
botUsername = resolved
|
||||||
|
}
|
||||||
|
}
|
||||||
var link *string
|
var link *string
|
||||||
if s.App.Cfg.TGBotUsername != "" && portalID != "" {
|
if botUsername != "" && portalID != "" {
|
||||||
v := "https://t.me/" + s.App.Cfg.TGBotUsername + "?start=" + portalID
|
v := "https://t.me/" + botUsername + "?start=" + portalID
|
||||||
link = &v
|
link = &v
|
||||||
}
|
}
|
||||||
var command *string
|
var command *string
|
||||||
@@ -81,7 +87,7 @@ func (s Server) accessMe(w http.ResponseWriter, r *http.Request) {
|
|||||||
"portal_user_id": nullablePlain(portalID),
|
"portal_user_id": nullablePlain(portalID),
|
||||||
"telegram_linked": emp != nil && emp.TGChatID != nil && *emp.TGChatID != "",
|
"telegram_linked": emp != nil && emp.TGChatID != nil && *emp.TGChatID != "",
|
||||||
"employee": emp,
|
"employee": emp,
|
||||||
"telegram_bot_username": nullablePlain(s.App.Cfg.TGBotUsername),
|
"telegram_bot_username": nullablePlain(botUsername),
|
||||||
"telegram_start_command": command,
|
"telegram_start_command": command,
|
||||||
"telegram_start_link": link,
|
"telegram_start_link": link,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,12 +9,15 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Telegram struct {
|
type Telegram struct {
|
||||||
token string
|
token string
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
mu sync.Mutex
|
||||||
|
username string
|
||||||
}
|
}
|
||||||
|
|
||||||
type tgResponse[T any] struct {
|
type tgResponse[T any] struct {
|
||||||
@@ -46,6 +49,11 @@ type TGUser struct {
|
|||||||
LastName string `json:"last_name"`
|
LastName string `json:"last_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TGBotUser struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewTelegram(token string) *Telegram {
|
func NewTelegram(token string) *Telegram {
|
||||||
return &Telegram{token: token, client: &http.Client{Timeout: 35 * time.Second}}
|
return &Telegram{token: token, client: &http.Client{Timeout: 35 * time.Second}}
|
||||||
}
|
}
|
||||||
@@ -54,6 +62,46 @@ func (t *Telegram) Enabled() bool {
|
|||||||
return strings.TrimSpace(t.token) != ""
|
return strings.TrimSpace(t.token) != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Telegram) BotUsername(ctx context.Context) (string, error) {
|
||||||
|
if !t.Enabled() {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
t.mu.Lock()
|
||||||
|
if t.username != "" {
|
||||||
|
username := t.username
|
||||||
|
t.mu.Unlock()
|
||||||
|
return username, nil
|
||||||
|
}
|
||||||
|
t.mu.Unlock()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, t.apiURL("getMe"), nil)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
resp, err := t.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
var out tgResponse[TGBotUser]
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if !out.OK {
|
||||||
|
return "", errors.New(out.Description)
|
||||||
|
}
|
||||||
|
username := strings.TrimPrefix(strings.TrimSpace(out.Result.Username), "@")
|
||||||
|
if username == "" {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
t.mu.Lock()
|
||||||
|
t.username = username
|
||||||
|
t.mu.Unlock()
|
||||||
|
return username, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Telegram) SendMessage(ctx context.Context, chatID string, text string) error {
|
func (t *Telegram) SendMessage(ctx context.Context, chatID string, text string) error {
|
||||||
if !t.Enabled() {
|
if !t.Enabled() {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user