I use go-telegram-bot-api for building a Telegram Bot.The logic is as follows: I send a link to the site with authorization in the tg bot, the user goes, logs in, and after clicking on the grant access button, it overstresses him back to the tg bot. I have a local server on which the handler function should set the url to go to telegram. But this does not happen.I implemented the handler using ServeHTTP, but I think that but for some reason is not called, so redirect does not happen.
type AuthorizationServer struct {
server *http.Server
pocketClient *pocket.Client
tokenRepository repository.TokenRepository
redirectUrl string
}
func NewAuthorizationServer(pocketClient *pocket.Client, tokenRepository repository.TokenRepository, redirectUrl string) *AuthorizationServer {
return &AuthorizationServer{pocketClient: pocketClient, tokenRepository: tokenRepository, redirectUrl: redirectUrl}
}
func (s *AuthorizationServer) Start() error {
s.server = &http.Server{
Addr: ":8080",
Handler: s,
}
return s.server.ListenAndServe()
}
func (s *AuthorizationServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusForbidden)
return
}
chatIDQuery := r.URL.Query().Get("chat_id")
if chatIDQuery == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
chatID, err := strconv.ParseInt(chatIDQuery, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
requestToken, err := s.tokenRepository.Get(chatID, repository.RequestTokens)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
authResp, err := s.pocketClient.Authorize(r.Context(), requestToken)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = s.tokenRepository.Save(chatID, authResp.AccessToken, repository.AccessTokens)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("Location", s.redirectUrl)// redirect
w.WriteHeader(http.StatusMovedPermanently) //
}
func (s *AuthorizationServer) createAccessToken(ctx context.Context, chatID int64) error {
requestToken, err := s.tokenRepository.Get(chatID, repository.RequestTokens)
if err != nil {
return err
}
authResp, err := s.pocketClient.Authorize(ctx, requestToken)
if err != nil {
return err
}
if err := s.tokenRepository.Save(chatID, authResp.AccessToken, repository.AccessTokens); err != nil {
return err
}
return nil
}
I expected the user to be redirected by my s.redirectUrl,that's how I set the Location in Header