aboutsummaryrefslogtreecommitdiff
path: root/examples/example_1.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <alexander@kiryukhin.su>2019-03-29 01:55:21 +0300
committerAlexander Kiryukhin <alexander@kiryukhin.su>2019-03-29 01:55:21 +0300
commit75679a0cbb631db348cb5a3bbe4a4f8aa5447f60 (patch)
tree714e1b09b0130cdbe893355cf2a382a5b9ec4330 /examples/example_1.go
parenta5f90182f76a279952f6cfd3dd7beab7391aaf56 (diff)
Первый рабочий релиз
Diffstat (limited to 'examples/example_1.go')
-rw-r--r--examples/example_1.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/examples/example_1.go b/examples/example_1.go
new file mode 100644
index 0000000..bdb496a
--- /dev/null
+++ b/examples/example_1.go
@@ -0,0 +1,50 @@
+package main
+
+import (
+ "fmt"
+ "github.com/neonxp/tamtam-go"
+ "log"
+ "net/http"
+ "os"
+)
+
+func main() {
+ // Initialisation
+ api := tamtam.New(os.Getenv("TOKEN"))
+
+ // Some methods demo:
+ info, err := api.GetMe()
+ log.Printf("Get me: %#v %#v", info, err)
+ chats, err := api.GetChats(0, 0)
+ log.Printf("Get chats: %#v %#v", chats, err)
+ chat, err := api.GetChat(chats.Chats[0].ChatId)
+ log.Printf("Get chat: %#v %#v", chat, err)
+ msgs, err := api.GetMessages(chats.Chats[0].ChatId, nil, 0, 0, 0)
+ log.Printf("Get messages: %#v %#v", msgs, err)
+
+ subscriptionResp, err := api.Subscribe(&tamtam.SubscriptionRequestBody{
+ Url: "https://4940675e.ngrok.io/webhook", // Replace by your webhook!
+ })
+ log.Printf("Subscription: %#v %#v", subscriptionResp, err)
+
+ ch := make(chan interface{}) // Channel with updates from TamTam
+
+ http.HandleFunc("/webhook", api.GetHandler(ch))
+ go func() {
+ for {
+ upd := <-ch
+ log.Printf("Received: %#v", upd)
+ switch upd := upd.(type) {
+ case tamtam.UpdateMessageCreated:
+ res, err := api.SendMessage(0, upd.Message.Sender.UserId, &tamtam.NewMessageBody{
+ Text: fmt.Sprintf("Hello, %s! Your message: %s", upd.Message.Sender.Name, upd.Message.Body.Text),
+ })
+ log.Printf("Answer: %#v %#v", res, err)
+ default:
+ log.Printf("Unknown type: %#v", upd)
+ }
+ }
+ }()
+
+ http.ListenAndServe(":10888", nil)
+}