Skip to content

Latest commit

 

History

History
129 lines (102 loc) · 2.93 KB

DESIGN.md

File metadata and controls

129 lines (102 loc) · 2.93 KB

Desgin

Requirements

  • Use context for cancelation and deadlines in IO.
  • Go way

Inspiration

Packages

Package Path Description
tg github.com/bots-house/go-tg Contains Client with methods and types used for sending and receiving messages.
tgr github.com/bots-house/go-tg/tgr Contains Bot and routing related types

Client

Construct a new Telegram Bot API client using token from @BotFather.

client := tg.NewClient("12345:secret")

Custumize client using options, e.g. use proxy for transport.

// create http.Client with proxy
doer := &http.Client{
  Transport: &http.Transport{
    Proxy: http.ProxyURL("socks5://127.0.0.1:9150")
  }
}

// create Telegram Bot API client with custom doer.
client := tg.NewClient("12345:secret", tg.WithDoer(doer))

Do

Do is method to execute any request to Telegram Bot API. It's low level.

// open local file for send
photo, err := tg.NewInputFileLocal("testdata/logo.png")
if err != nil {
  // ...
}
defer photo.Close()

// create request
req := tg.NewRequest("sendPhoto")
req.AddPeer("chat_id", tg.Username("go_tg"))
req.AddFile("photo", photo)
req.AddString("caption", "this is our logo")
req.AddParseMode(tg.Markdown)

if err := client.Do(ctx, req, nil); err != nil {
  // ...
}
me, err := client.GetMe(ctx)
if err != nil {
  // ...
}
opts := &tg.SendTextOpts{
  ParseMode: tg.Markdown,
  DisableNotification: true,
}

msg, err := client.SendText(
  ctx,
  tg.Username("MrLinch"),
  "how are u?",
  opts,
)
opts := &tg.PromoteChatMemberOpts{
  CanChangeInfo: true,
  CanPostMessages: true,
}

if err := client.PromoteChatMember(ctx,
  tg.Username("oh_my_channely"),
  tg.UserID(12341920),
  opts,
); err != nil {
  // ...
}
if err := client.SetMyCommands(ctx, []tg.BotCommand{
  {"start", "get current stats"},
}); err != nil {
  // ...
}