Skip to content

Commit

Permalink
Merge pull request #3 from helios-ag/cb-example-new
Browse files Browse the repository at this point in the history
added callback example
  • Loading branch information
helios-ag authored Jun 26, 2022
2 parents c3a0670 + 53fff17 commit 6382f98
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
71 changes: 55 additions & 16 deletions examples/callback/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 6382f98

Please sign in to comment.