diff --git a/examples/callback/main.go b/examples/callback/main.go index 66078c7..517e9e9 100644 --- a/examples/callback/main.go +++ b/examples/callback/main.go @@ -6,6 +6,7 @@ import ( "github.com/AlecAivazis/survey/v2" tgstat "github.com/helios-ag/tgstat-go" "github.com/helios-ag/tgstat-go/callback" + "net/http" "os" ) @@ -15,16 +16,22 @@ var qs = []*survey.Question{ Prompt: &survey.Input{Message: "Enter your token"}, Validate: survey.Required, }, + { + Name: "CallbackURL", + Prompt: &survey.Input{Message: "Enter callback url"}, + }, { Name: "ChannelId", - Prompt: &survey.Input{Message: "Enter Channel ID"}, + Prompt: &survey.Input{Message: "Enter ChannelId"}, }, } +// Simple example that can be used with ngrok for testing purposes func main() { answers := struct { - Token string - ChannelId string + Token string + CallbackURL string + ChannelId string }{} err := survey.Ask(qs, &answers) @@ -35,21 +42,53 @@ func main() { tgstat.Token = answers.Token - req := callback.SubscribeChannelRequest{ - SubscriptionId: nil, - ChannelId: "", - EventTypes: "", - } + callbackReq := "" - sub, _, err := callback.SubscribeChannel(context.Background(), req) + http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) { + cbReq, res, setErr := callback.SetCallback(context.Background(), answers.CallbackURL) + callbackReq = cbReq.VerifyCode + fmt.Fprint(w, callbackReq) + if setErr != nil { + fmt.Printf("error setting callBack: %v\n", setErr) + fmt.Printf("status: %v\n", res.Status) + fmt.Printf("status: %d\n", res.StatusCode) + fmt.Printf("status: %v\n", res.Body) + os.Exit(1) + } + }) - if err != nil { - fmt.Printf("error getting data: %v\n", err) - os.Exit(1) - } + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodPost: + fmt.Fprint(w, callbackReq) + } + }) + + http.HandleFunc("/result", func(w http.ResponseWriter, r *http.Request) { + resp, _, errInfo := callback.GetCallbackInfo(context.Background()) + if errInfo == nil { + fmt.Fprintf(w, resp.Status) + fmt.Fprintf(w, resp.Response.Url) + fmt.Fprintf(w, resp.Response.LastErrorMessage) + fmt.Fprint(w, resp.Response.LastErrorDate) + fmt.Fprint(w, resp.Response.PendingUpdateCount) + + } + fmt.Fprintf(w, "error getting callBack info: %v\n", errInfo) + }) - fmt.Print("Subscription ID") - fmt.Printf("Title: %d\n", sub.SubscriptionId) + http.HandleFunc("/subscribe", func(w http.ResponseWriter, r *http.Request) { + request := callback.SubscribeChannelRequest{ + ChannelId: answers.ChannelId, + EventTypes: "new_post", + } + resp, _, errInfo := callback.SubscribeChannel(context.Background(), request) + if errInfo != nil { + fmt.Fprintf(w, "error subscribing: %v\n", errInfo) + } else { + fmt.Fprint(w, fmt.Sprint(resp.SubscriptionId)) + } + }) - os.Exit(0) + http.ListenAndServe(":8081", nil) } diff --git a/go.mod b/go.mod index f0b9628..c08fb01 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/AlecAivazis/survey/v2 v2.3.4 - github.com/helios-ag/tgstat-go v0.0.0-20220522063232-7e04b77ca447 + github.com/helios-ag/tgstat-go v0.0.0-20220626084129-5b1b105c55fc ) require ( diff --git a/go.sum b/go.sum index fbdea5a..19c1dcf 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/helios-ag/tgstat-go v0.0.0-20220514025444-1ff93f879e98 h1:AICOS3vX6gY github.com/helios-ag/tgstat-go v0.0.0-20220514025444-1ff93f879e98/go.mod h1:A3zy1r63M9rGfHgu0bchihyyYdcYlRpc44Mw1prIqfM= github.com/helios-ag/tgstat-go v0.0.0-20220522063232-7e04b77ca447 h1:fXNZmESUIWYc8c9+48MOTqv/co86PZWUKIsk8wDk/yU= github.com/helios-ag/tgstat-go v0.0.0-20220522063232-7e04b77ca447/go.mod h1:8tMmg1tM4QLN4IJjm3garwD/3lTW1sbtiB1hSw8VnRs= +github.com/helios-ag/tgstat-go v0.0.0-20220626084129-5b1b105c55fc h1:1cwp8m+TE/N8N8rOYF8b+2ZnsO2wCasLk8WqDKicfww= +github.com/helios-ag/tgstat-go v0.0.0-20220626084129-5b1b105c55fc/go.mod h1:8tMmg1tM4QLN4IJjm3garwD/3lTW1sbtiB1hSw8VnRs= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=