From 8046e5a6143b639b591c4b56f763da55fe3fdf08 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 26 Jun 2022 23:32:06 +1000 Subject: [PATCH 1/3] added callback example --- examples/callback/main.go | 71 ++++++++++++++++++++++++++++++--------- go.mod | 2 +- go.sum | 2 ++ 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/examples/callback/main.go b/examples/callback/main.go index 66078c7..53b3cc9 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.Fprintf(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.Fprintf(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.Fprintf(w, fmt.Sprintf("%d", resp.Response.LastErrorDate)) + fmt.Fprintf(w, fmt.Sprintf("%d", 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.Fprintf(w, fmt.Sprintf("%d", 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= From 34cb260a93c4dd8035e5ef8681ae9b5e2020312a Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 26 Jun 2022 23:40:12 +1000 Subject: [PATCH 2/3] staticcheck --- examples/callback/main.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/callback/main.go b/examples/callback/main.go index 53b3cc9..dcc8527 100644 --- a/examples/callback/main.go +++ b/examples/callback/main.go @@ -47,7 +47,7 @@ func main() { http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) { cbReq, res, setErr := callback.SetCallback(context.Background(), answers.CallbackURL) callbackReq = cbReq.VerifyCode - fmt.Fprintf(w, callbackReq) + fmt.Fprint(w, callbackReq) if setErr != nil { fmt.Printf("error setting callBack: %v\n", setErr) fmt.Printf("status: %v\n", res.Status) @@ -60,7 +60,7 @@ func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: - fmt.Fprintf(w, callbackReq) + fmt.Fprint(w, callbackReq) } }) @@ -70,8 +70,8 @@ func main() { fmt.Fprintf(w, resp.Status) fmt.Fprintf(w, resp.Response.Url) fmt.Fprintf(w, resp.Response.LastErrorMessage) - fmt.Fprintf(w, fmt.Sprintf("%d", resp.Response.LastErrorDate)) - fmt.Fprintf(w, fmt.Sprintf("%d", resp.Response.PendingUpdateCount)) + fmt.Fprintf(w, fmt.Sprint(resp.Response.LastErrorDate)) + fmt.Fprintf(w, fmt.Sprint(resp.Response.PendingUpdateCount)) } fmt.Fprintf(w, "error getting callBack info: %v\n", errInfo) @@ -86,7 +86,7 @@ func main() { if errInfo != nil { fmt.Fprintf(w, "error subscribing: %v\n", errInfo) } else { - fmt.Fprintf(w, fmt.Sprintf("%d", resp.SubscriptionId)) + fmt.Fprintf(w, fmt.Sprint(resp.SubscriptionId)) } }) From 53fff17558f514f162b42e1283cd26b36f88dee9 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 26 Jun 2022 23:56:58 +1000 Subject: [PATCH 3/3] fix --- examples/callback/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/callback/main.go b/examples/callback/main.go index dcc8527..517e9e9 100644 --- a/examples/callback/main.go +++ b/examples/callback/main.go @@ -70,8 +70,8 @@ func main() { fmt.Fprintf(w, resp.Status) fmt.Fprintf(w, resp.Response.Url) fmt.Fprintf(w, resp.Response.LastErrorMessage) - fmt.Fprintf(w, fmt.Sprint(resp.Response.LastErrorDate)) - fmt.Fprintf(w, fmt.Sprint(resp.Response.PendingUpdateCount)) + fmt.Fprint(w, resp.Response.LastErrorDate) + fmt.Fprint(w, resp.Response.PendingUpdateCount) } fmt.Fprintf(w, "error getting callBack info: %v\n", errInfo) @@ -86,7 +86,7 @@ func main() { if errInfo != nil { fmt.Fprintf(w, "error subscribing: %v\n", errInfo) } else { - fmt.Fprintf(w, fmt.Sprint(resp.SubscriptionId)) + fmt.Fprint(w, fmt.Sprint(resp.SubscriptionId)) } })