mirror of https://github.com/tucnak/telebot
Minimal auth functionality: Create()
parent
45451c0de3
commit
4bb4333bb1
@ -0,0 +1,9 @@
|
|||||||
|
package telebot
|
||||||
|
|
||||||
|
type AuthError struct {
|
||||||
|
Payload string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e AuthError) Error() string {
|
||||||
|
return "AuthError: " + e.Payload
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package telebot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Bot struct {
|
||||||
|
Token string
|
||||||
|
|
||||||
|
// A "user" behind bot instance
|
||||||
|
Identity User
|
||||||
|
}
|
||||||
|
|
||||||
|
func Create(token string) (Bot, error) {
|
||||||
|
request := "https://api.telegram.org/bot" + token + "/getMe"
|
||||||
|
|
||||||
|
resp, err := http.Get(request)
|
||||||
|
if err != nil {
|
||||||
|
return Bot{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
me_json, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return Bot{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var bot_info struct {
|
||||||
|
Ok bool
|
||||||
|
Result User
|
||||||
|
Description string
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(me_json, &bot_info)
|
||||||
|
if err != nil {
|
||||||
|
return Bot{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if bot_info.Ok {
|
||||||
|
return Bot{
|
||||||
|
Token: token,
|
||||||
|
Identity: bot_info.Result,
|
||||||
|
}, nil
|
||||||
|
} else {
|
||||||
|
return Bot{}, AuthError{bot_info.Description}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package telebot
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
const TESTING_TOKEN = "107177593:AAHBJfF3nv3pZXVjXpoowVhv_KSGw56s8zo"
|
||||||
|
|
||||||
|
func TestCreate(t *testing.T) {
|
||||||
|
_, err := Create(TESTING_TOKEN)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue