aboutsummaryrefslogtreecommitdiff
path: root/examples/example_1.go
blob: bdb496a781c4e38984ede46a0fdee5f404aaa5aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)
}