aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2021-02-14 13:21:37 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2021-02-14 13:21:37 +0300
commite768ef944c88be1b18c995544c76fa931d9c5916 (patch)
tree5d1683dbe87fedbc6c1d7376df7147a8ad0936c2
parentee63b08bd7861702123f9c5e83d60f49b1805e85 (diff)
Fix issue #3
-rw-r--r--api.go3
-rw-r--r--client.go7
-rw-r--r--examples/example_longpolling.go7
-rw-r--r--examples/example_webhook.go3
4 files changed, 16 insertions, 4 deletions
diff --git a/api.go b/api.go
index 77fe2dc..5e1421b 100644
--- a/api.go
+++ b/api.go
@@ -161,6 +161,9 @@ func (a *Api) getUpdates(limit int, timeout int, marker int64, types []string) (
}
body, err := a.client.request(http.MethodGet, "updates", values, nil)
if err != nil {
+ if err == errLongPollTimeout {
+ return result, nil
+ }
return result, err
}
defer func() {
diff --git a/client.go b/client.go
index 14f7129..0610e22 100644
--- a/client.go
+++ b/client.go
@@ -3,6 +3,7 @@ package tamtam
import (
"bytes"
"encoding/json"
+ "errors"
"io"
"net/http"
"net/url"
@@ -10,6 +11,10 @@ import (
"github.com/neonxp/tamtam/schemes"
)
+var (
+ errLongPollTimeout = errors.New("timeout")
+)
+
type client struct {
key string
version string
@@ -44,7 +49,7 @@ func (cl *client) requestReader(method, path string, query url.Values, body io.R
err, ok := err.(*url.Error)
if ok {
if err.Timeout() {
- return nil, nil
+ return nil, errLongPollTimeout
}
}
return nil, err
diff --git a/examples/example_longpolling.go b/examples/example_longpolling.go
index 6bb4727..63e0632 100644
--- a/examples/example_longpolling.go
+++ b/examples/example_longpolling.go
@@ -13,6 +13,7 @@ import (
"os/signal"
"github.com/neonxp/tamtam"
+ "github.com/neonxp/tamtam/schemes"
)
func main() {
@@ -28,13 +29,15 @@ func main() {
for upd := range api.GetUpdates(ctx) {
log.Printf("Received: %#v", upd)
switch upd := upd.(type) {
- case *tamtam.MessageCreatedUpdate:
+ case *schemes.MessageCreatedUpdate:
err := api.Messages.Send(
tamtam.NewMessage().
SetUser(upd.Message.Sender.UserId).
SetText(fmt.Sprintf("Hello, %s! Your message: %s", upd.Message.Sender.Name, upd.Message.Body.Text)),
)
- log.Printf("Answer: %#v %#v", res, err)
+ if err != nil {
+ log.Printf("Error: %#v", err)
+ }
default:
log.Printf("Unknown type: %#v", upd)
}
diff --git a/examples/example_webhook.go b/examples/example_webhook.go
index b4857c1..2709e6f 100644
--- a/examples/example_webhook.go
+++ b/examples/example_webhook.go
@@ -12,6 +12,7 @@ import (
"os"
"github.com/neonxp/tamtam"
+ "github.com/neonxp/tamtam/schemes"
)
func main() {
@@ -38,7 +39,7 @@ func main() {
upd := <-ch
log.Printf("Received: %#v", upd)
switch upd := upd.(type) {
- case tamtam.MessageCreatedUpdate:
+ case *schemes.MessageCreatedUpdate:
err := api.Messages.Send(
tamtam.NewMessage().
SetUser(upd.Message.Sender.UserId).