aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-08-09 02:02:05 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-08-09 02:02:05 +0300
commit94c96cef29cd83bc60b2eabb8bb89c9fd8c7059b (patch)
tree676b3e2b6ffb171e102931e5fac01f7a4621c20a /examples
parentd13acd7da9bd1c08de33b91120a479df88d21db2 (diff)
0.1.8 Release
Diffstat (limited to 'examples')
-rw-r--r--examples/example.go53
-rw-r--r--examples/example_longpolling.go (renamed from examples/example_2.go)24
-rw-r--r--examples/example_webhook.go (renamed from examples/example_1.go)25
3 files changed, 72 insertions, 30 deletions
diff --git a/examples/example.go b/examples/example.go
new file mode 100644
index 0000000..b4e84ac
--- /dev/null
+++ b/examples/example.go
@@ -0,0 +1,53 @@
+// +build ignore
+
+package main
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "os"
+
+ "github.com/neonxp/tamtam"
+)
+
+func main() {
+ api := tamtam.New(os.Getenv("TOKEN"))
+
+ info, err := api.Bots.GetBot() // Простой метод
+ log.Printf("Get me: %#v %#v", info, err)
+ go api.UpdatesLoop(context.Background()) // Запуск цикла получения обновлений
+ for upd := range api.GetUpdates() { // Чтение из канала с обновлениями
+ log.Printf("Received: %#v", upd)
+ switch upd := upd.(type) { // Определение типа пришедшего обновления
+ case *tamtam.MessageCreatedUpdate:
+ // Создание клавиатуры
+ keyboard := api.Messages.NewKeyboardBuilder()
+ keyboard.
+ AddRow().
+ AddGeolocation("Прислать геолокацию", true).
+ AddContact("Прислать контакт")
+ keyboard.
+ AddRow().
+ AddLink("Библиотека", tamtam.POSITIVE, "https://github.com/neonxp/tamtam").
+ AddCallback("Колбек 1", tamtam.NEGATIVE, "callback_1").
+ AddCallback("Колбек 2", tamtam.NEGATIVE, "callback_2")
+
+ // Отправка сообщения с клавиатурой
+ res, err := api.Messages.SendMessage(0, upd.Message.Sender.UserId, &tamtam.NewMessageBody{
+ Text: fmt.Sprintf("Hello, %s! Your message: %s", upd.Message.Sender.Name, upd.Message.Body.Text),
+ Attachments: []interface{}{
+ tamtam.NewInlineKeyboardAttachmentRequest(keyboard.Build()),
+ },
+ })
+ log.Printf("Answer: %#v %#v", res, err)
+ case *tamtam.MessageCallbackUpdate:
+ res, err := api.Messages.SendMessage(0, upd.Callback.User.UserId, &tamtam.NewMessageBody{
+ Text: "Callback: " + upd.Callback.Payload,
+ })
+ log.Printf("Answer: %#v %#v", res, err)
+ default:
+ log.Printf("Unknown type: %#v", upd)
+ }
+ }
+}
diff --git a/examples/example_2.go b/examples/example_longpolling.go
index 74547fa..67a0d7c 100644
--- a/examples/example_2.go
+++ b/examples/example_longpolling.go
@@ -8,10 +8,11 @@ package main
import (
"context"
"fmt"
- "github.com/neonxp/tamtam"
"log"
"os"
"os/signal"
+
+ "github.com/neonxp/tamtam"
)
func main() {
@@ -19,28 +20,18 @@ func main() {
api := tamtam.New(os.Getenv("TOKEN"))
// Some methods demo:
- info, err := api.GetMe()
+ info, err := api.Bots.GetBot()
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)
- subs, _ := api.GetSubscriptions()
- for _, s := range subs.Subscriptions {
- _, _ = api.Unsubscribe(s.Url)
- }
- ch := make(chan interface{}, 1) // Channel with updates from TamTam
ctx, cancel := context.WithCancel(context.Background())
-
go func() {
for {
select {
- case upd := <-ch:
+ case upd := <-api.GetUpdates():
log.Printf("Received: %#v", upd)
switch upd := upd.(type) {
- case tamtam.UpdateMessageCreated:
- res, err := api.SendMessage(0, upd.Message.Sender.UserId, &tamtam.NewMessageBody{
+ case *tamtam.MessageCreatedUpdate:
+ res, err := api.Messages.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)
@@ -53,7 +44,6 @@ func main() {
}
}()
-
go func() {
exit := make(chan os.Signal)
signal.Notify(exit, os.Kill, os.Interrupt)
@@ -65,7 +55,7 @@ func main() {
}
}()
- if err := api.GetUpdatesLoop(ctx, ch); err != nil {
+ if err := api.UpdatesLoop(ctx); err != nil {
log.Fatalln(err)
}
diff --git a/examples/example_1.go b/examples/example_webhook.go
index 5c03063..ad1bdae 100644
--- a/examples/example_1.go
+++ b/examples/example_webhook.go
@@ -1,3 +1,5 @@
+// +build ignore
+
/**
* Webhook example
*/
@@ -5,30 +7,27 @@ package main
import (
"fmt"
- "github.com/neonxp/tamtam"
"log"
"net/http"
"os"
+
+ "github.com/neonxp/tamtam"
)
func main() {
// Initialisation
api := tamtam.New(os.Getenv("TOKEN"))
+ host := os.Getenv("HOST")
// Some methods demo:
- info, err := api.GetMe()
+ info, err := api.Bots.GetBot()
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)
- subs, _ := api.GetSubscriptions()
+
+ subs, _ := api.Subscriptions.GetSubscriptions()
for _, s := range subs.Subscriptions {
- _, _ = api.Unsubscribe(s.Url)
+ _, _ = api.Subscriptions.Unsubscribe(s.Url)
}
- subscriptionResp, err := api.Subscribe("https://576df2ec.ngrok.io/webhook", []string{})
+ subscriptionResp, err := api.Subscriptions.Subscribe(host+"/webhook", []string{})
log.Printf("Subscription: %#v %#v", subscriptionResp, err)
ch := make(chan interface{}) // Channel with updates from TamTam
@@ -39,8 +38,8 @@ func main() {
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{
+ case tamtam.MessageCreatedUpdate:
+ res, err := api.Messages.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)