|
| 1 | +package maxigo_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/maxigo-bot/maxigo-client" |
| 12 | +) |
| 13 | + |
| 14 | +func ExampleNew() { |
| 15 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 16 | + _ = json.NewEncoder(w).Encode(maxigo.BotInfo{ |
| 17 | + UserWithPhoto: maxigo.UserWithPhoto{ |
| 18 | + User: maxigo.User{UserID: 1, FirstName: "TestBot", IsBot: true}, |
| 19 | + }, |
| 20 | + }) |
| 21 | + })) |
| 22 | + defer srv.Close() |
| 23 | + |
| 24 | + client, err := maxigo.New("test-token", maxigo.WithBaseURL(srv.URL)) |
| 25 | + if err != nil { |
| 26 | + fmt.Println("error:", err) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + bot, err := client.GetBot(context.Background()) |
| 31 | + if err != nil { |
| 32 | + fmt.Println("error:", err) |
| 33 | + return |
| 34 | + } |
| 35 | + fmt.Println(bot.FirstName) |
| 36 | + // Output: TestBot |
| 37 | +} |
| 38 | + |
| 39 | +func ExampleClient_SendMessage() { |
| 40 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 41 | + resp := struct { |
| 42 | + Message maxigo.Message `json:"message"` |
| 43 | + }{ |
| 44 | + Message: maxigo.Message{ |
| 45 | + Body: maxigo.MessageBody{MID: "mid-1", Text: strPtr("Hello!")}, |
| 46 | + }, |
| 47 | + } |
| 48 | + _ = json.NewEncoder(w).Encode(resp) |
| 49 | + })) |
| 50 | + defer srv.Close() |
| 51 | + |
| 52 | + client, _ := maxigo.New("test-token", maxigo.WithBaseURL(srv.URL)) |
| 53 | + msg, err := client.SendMessage(context.Background(), 12345, &maxigo.NewMessageBody{ |
| 54 | + Text: maxigo.Some("Hello!"), |
| 55 | + }) |
| 56 | + if err != nil { |
| 57 | + fmt.Println("error:", err) |
| 58 | + return |
| 59 | + } |
| 60 | + fmt.Println(*msg.Body.Text) |
| 61 | + // Output: Hello! |
| 62 | +} |
| 63 | + |
| 64 | +func ExampleClient_AnswerCallback() { |
| 65 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 66 | + _ = json.NewEncoder(w).Encode(maxigo.SimpleQueryResult{Success: true}) |
| 67 | + })) |
| 68 | + defer srv.Close() |
| 69 | + |
| 70 | + client, _ := maxigo.New("test-token", maxigo.WithBaseURL(srv.URL)) |
| 71 | + result, err := client.AnswerCallback(context.Background(), "cb-123", &maxigo.CallbackAnswer{ |
| 72 | + Notification: maxigo.Some("Done!"), |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + fmt.Println("error:", err) |
| 76 | + return |
| 77 | + } |
| 78 | + fmt.Println(result.Success) |
| 79 | + // Output: true |
| 80 | +} |
| 81 | + |
| 82 | +func ExampleClient_UploadPhoto() { |
| 83 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 84 | + switch r.URL.Path { |
| 85 | + case "/uploads": |
| 86 | + // GetUploadURL — return the same server |
| 87 | + _ = json.NewEncoder(w).Encode(maxigo.UploadEndpoint{ |
| 88 | + URL: "http://" + r.Host + "/do-upload", |
| 89 | + }) |
| 90 | + default: |
| 91 | + // Actual upload |
| 92 | + _ = json.NewEncoder(w).Encode(maxigo.PhotoTokens{ |
| 93 | + Photos: map[string]maxigo.PhotoToken{"default": {Token: "photo-tok"}}, |
| 94 | + }) |
| 95 | + } |
| 96 | + })) |
| 97 | + defer srv.Close() |
| 98 | + |
| 99 | + client, _ := maxigo.New("test-token", maxigo.WithBaseURL(srv.URL)) |
| 100 | + tokens, err := client.UploadPhoto(context.Background(), "photo.jpg", strings.NewReader("image data")) |
| 101 | + if err != nil { |
| 102 | + fmt.Println("error:", err) |
| 103 | + return |
| 104 | + } |
| 105 | + fmt.Println(tokens.Photos["default"].Token) |
| 106 | + // Output: photo-tok |
| 107 | +} |
| 108 | + |
| 109 | +func ExampleNewCallbackButton() { |
| 110 | + btn := maxigo.NewCallbackButton("OK", "confirm") |
| 111 | + fmt.Printf("type=%s text=%s payload=%s\n", btn.Type, btn.Text, btn.Payload) |
| 112 | + // Output: type=callback text=OK payload=confirm |
| 113 | +} |
| 114 | + |
| 115 | +func ExampleNewLinkButton() { |
| 116 | + btn := maxigo.NewLinkButton("Open", "https://example.com") |
| 117 | + fmt.Printf("type=%s url=%s\n", btn.Type, btn.URL) |
| 118 | + // Output: type=link url=https://example.com |
| 119 | +} |
| 120 | + |
| 121 | +func ExampleSome() { |
| 122 | + opt := maxigo.Some("hello") |
| 123 | + fmt.Printf("set=%t value=%s\n", opt.Set, opt.Value) |
| 124 | + |
| 125 | + var empty maxigo.OptString |
| 126 | + fmt.Printf("set=%t value=%q\n", empty.Set, empty.Value) |
| 127 | + // Output: |
| 128 | + // set=true value=hello |
| 129 | + // set=false value="" |
| 130 | +} |
| 131 | + |
| 132 | +func ExampleNewInlineKeyboardAttachment() { |
| 133 | + kb := maxigo.NewInlineKeyboardAttachment([][]maxigo.Button{ |
| 134 | + { |
| 135 | + maxigo.NewCallbackButton("Yes", "yes"), |
| 136 | + maxigo.NewCallbackButton("No", "no"), |
| 137 | + }, |
| 138 | + }) |
| 139 | + fmt.Println(kb.Type) |
| 140 | + // Output: inline_keyboard |
| 141 | +} |
| 142 | + |
| 143 | +func strPtr(s string) *string { return &s } |
0 commit comments