Skip to content

Commit

Permalink
Add option for custom api server.
Browse files Browse the repository at this point in the history
  • Loading branch information
onrik committed May 29, 2021
1 parent ba2f3d4 commit 08c43d4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Client lib for [Telegram bot api](https://core.telegram.org/bots/api). Supports **Bot API v2.3.1** (of 4th Dec 2016).

##### Simple echo bot example:
### Simple echo bot
```go
package main

Expand All @@ -21,7 +21,8 @@ import (
func main() {
bot, err := micha.NewBot("<token>")
if err != nil {
log.Fatal(err)
log.Println(err)
return
}

go bot.Start()
Expand All @@ -34,3 +35,42 @@ func main() {
}

```


### Custom [Telegram Bot API](https://github.com/tdlib/telegram-bot-api)
```go
package main

import (
"log"

"github.com/onrik/micha"
)

func main() {
bot, err := micha.NewBot(
"<token>",
micha.WithAPIServer("http://127.0.0.1:8081"),
)
if err != nil {
log.Println(err)
return
}

err = bot.Logout()
if err != nil {
log.Println(err)
return
}


go bot.Start()

for update := range bot.Updates() {
if update.Message != nil {
bot.SendMessage(update.Message.Chat.ID, update.Message.Text, nil)
}
}
}

```
32 changes: 27 additions & 5 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

const (
API_URL = "https://api.telegram.org/bot%s/%s"
FILE_API_URL = "https://api.telegram.org/file/bot%s/%s"
defaultAPIServer = "https://api.telegram.org"
)

type Response struct {
Expand All @@ -37,7 +36,8 @@ func NewBot(token string, opts ...Option) (*Bot, error) {
options := Options{
limit: 100,
timeout: 25,
logger: newLogger("micha"),
logger: newLogger("[micha] "),
apiServer: defaultAPIServer,
httpClient: http.DefaultClient,
}

Expand All @@ -63,7 +63,7 @@ func NewBot(token string, opts ...Option) (*Bot, error) {

// Build url for API method
func (bot *Bot) buildURL(method string) string {
return fmt.Sprintf(API_URL, bot.token, method)
return bot.Options.apiServer + fmt.Sprintf("/bot%s/%s", bot.token, method)
}

// Decode response result to target object
Expand Down Expand Up @@ -230,6 +230,28 @@ func (bot *Bot) DeleteWebhook() error {
return bot.post("deleteWebhook", nil, nil)
}

// Logout
// Use this method to log out from the cloud Bot API server before launching the bot locally.
// You must log out the bot before running it locally,
// otherwise there is no guarantee that the bot will receive updates.
// After a successful call, you can immediately log in on a local server,
// but will not be able to log in back to the cloud Bot API server for 10 minutes.
func (bot *Bot) Logout() error {
url := defaultAPIServer + fmt.Sprintf("/bot%s/logOut", bot.token)
request, err := newGetRequest(url, nil)
if err != nil {
return err
}

response, err := bot.httpClient.Do(request)
if err != nil {
return err
}

_, err = handleResponse(response)
return err
}

// A simple method for testing your bot's auth token.
// Returns basic information about the bot in form of a User object.
func (bot *Bot) GetMe() (*User, error) {
Expand Down Expand Up @@ -558,7 +580,7 @@ func (bot *Bot) GetFile(fileID string) (*File, error) {

// Return absolute url for file downloading by file path
func (bot *Bot) DownloadFileURL(filePath string) string {
return fmt.Sprintf(FILE_API_URL, bot.token, filePath)
return bot.Options.apiServer + fmt.Sprintf("/file/bot%s/%s", bot.token, filePath)
}

// Use this method to edit text messages sent by the bot or via the bot (for inline bots).
Expand Down
3 changes: 2 additions & 1 deletion bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (s *BotTestSuite) SetupSuite() {
limit: 100,
timeout: 25,
logger: newLogger("micha"),
apiServer: defaultAPIServer,
httpClient: http.DefaultClient,
},
}
Expand Down Expand Up @@ -129,7 +130,7 @@ func (s *BotTestSuite) TestNewBot() {
s.Require().NotNil(bot)
s.Require().Equal(25, bot.timeout)
s.Require().Equal(100, bot.limit)
s.Require().Equal(newLogger("micha"), bot.logger)
s.Require().Equal(newLogger("[micha] "), bot.logger)

// With options
logger := log.New(os.Stderr, "", log.LstdFlags)
Expand Down
10 changes: 10 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package micha

import "strings"

type Options struct {
limit int
timeout int
logger Logger
apiServer string
httpClient HttpClient
}

Expand Down Expand Up @@ -39,6 +42,13 @@ func WithHttpClient(httpClient HttpClient) Option {
}
}

// WithAPIServer - set custom api server (https://github.com/tdlib/telegram-bot-api)
func WithAPIServer(url string) Option {
return func(o *Options) {
o.apiServer = strings.TrimSuffix(url, "/")
}
}

// SendMessageOptions optional params SendMessage method
type SendMessageOptions struct {
ParseMode ParseMode `json:"parse_mode,omitempty"`
Expand Down

0 comments on commit 08c43d4

Please sign in to comment.