{"id":13409305,"url":"https://github.com/olebedev/go-tgbot","last_synced_at":"2025-08-04T18:07:45.472Z","repository":{"id":57483023,"uuid":"76157510","full_name":"olebedev/go-tgbot","owner":"olebedev","description":"Golang  telegram bot API wrapper, session-based router and middleware","archived":false,"fork":false,"pushed_at":"2018-06-25T04:50:26.000Z","size":668,"stargazers_count":120,"open_issues_count":2,"forks_count":5,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-15T06:14:28.272Z","etag":null,"topics":["api","bot","easyjson","middleware","router","swagger","telegram"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/olebedev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-11T06:06:32.000Z","updated_at":"2024-11-06T11:54:18.000Z","dependencies_parsed_at":"2022-08-27T21:02:05.909Z","dependency_job_id":null,"html_url":"https://github.com/olebedev/go-tgbot","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olebedev%2Fgo-tgbot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olebedev%2Fgo-tgbot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olebedev%2Fgo-tgbot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olebedev%2Fgo-tgbot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/olebedev","download_url":"https://codeload.github.com/olebedev/go-tgbot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249016641,"owners_count":21198833,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","bot","easyjson","middleware","router","swagger","telegram"],"created_at":"2024-07-30T20:00:59.741Z","updated_at":"2025-04-15T06:14:33.544Z","avatar_url":"https://github.com/olebedev.png","language":"Go","funding_links":[],"categories":["Bot Building","Bot建设","Third-party APIs","机器人相关","机器人相关` 构建和使用机器人的库`","Libraries","第三方API","\u003cspan id=\"第三方api-third-party-apis\"\u003e第三方API Third-party APIs\u003c/span\u003e","Uncategorized"],"sub_categories":["Contents","Free e-books","Go","Advanced Console UIs","HTTP Clients","交流","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"# go-tgbot [![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/olebedev/go-tgbot)\n\n\u003e Pure Golang telegram bot API wrapper generated from swagger definition, session-based routing and middlewares.\n\n### Usage benefits\n\n1. **No need to learn any other library API.** You will use methods with payload exactly like it presented on telegram bot API description page. With only couple trade-offs, b/c of telegram bot API is generics a bit.\n2. **All models and methods are being supported.** The models and methods were generated from `swagger.yaml` description file. So, new entities/methods could be added by describing in the YAML swagger file. This approach allows validating the description, avoid typos and develop fast.\n3. `easyjson` is plugged. So, **it's fast**.\n4. **`context.Context` based** HTTP client\n5. **Session-based routing**, not only message text based.\n\n### Client\n\nClient package could be used as regular `go-swagger` client library without using Router. There are the only two additional features over `go-swagger`, a possibility to setup token by default(It solved as an [interface checking](https://github.com/olebedev/go-tgbot/blob/master/client.go#L36)) and [API throttling](https://github.com/olebedev/go-tgbot/blob/master/client.go#L26) for 30 calls per second([see more info](https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this)). \n\nExample:\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"flag\"\n\t\"log\"\n\t\"time\"\n\n\ttgbot \"github.com/olebedev/go-tgbot\"\n\t\"github.com/olebedev/go-tgbot/client/users\"\n)\n\nvar token *string\n\nfunc main() {\n\ttoken = flag.String(\"token\", \"\", \"telegram bot token\")\n\tflag.Parse()\n\n\tctx, cancel := context.WithCancel(context.Background())\n\tdefer cancel()\n\n\tapi := tgbot.NewClient(ctx, *token)\n\n\tlog.Println(api.Users.GetMe(nil))\n\n\t// Also, every calls could be done with given context\n\tctx, cancel = context.WithTimeout(ctx, 10*time.Second)\n\tdefer cancel()\n\n\t_, err := api.Users.GetMe(\n\t\tusers.NewGetMeParams().\n\t\t\tWithContext(ctx),\n\t)\n\n\tif err != nil {\n\t\t// users.NewGetMeBadRequest()\n\t\tif e, ok := err.(*users.GetMeBadRequest); ok {\n\t\t\tlog.Println(e.Payload.ErrorCode, e.Payload.Description)\n\t\t}\n\t}\n}\n```\n\nSince swagger covers many other platforms/technologies the same libraries could be generated for them too. See the source here - [`swagger.yaml`](https://github.com/olebedev/go-tgbot/blob/master/swagger.yaml).  \nAlso, have a look at [Swagger UI for telegram API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/olebedev/go-tgbot/master/swagger.yaml#/)(`master` branch).\n\n### Router\n\nThe Router allows binding between kinds of updates and handlers, which are being checked via regexp. Router includes client API library as embedded struct. Example:\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"flag\"\n\t\"log\"\n\t\"os\"\n\n\ttgbot \"github.com/olebedev/go-tgbot\"\n\t\"github.com/olebedev/go-tgbot/client/messages\"\n\t\"github.com/olebedev/go-tgbot/models\"\n)\n\nvar token *string\n\nfunc main() {\n\ttoken = flag.String(\"token\", \"\", \"telegram bot token\")\n\tflag.Parse()\n\n\tctx, cancel := context.WithCancel(context.Background())\n\tdefer cancel()\n\n\tr := tgbot.New(ctx, *token)\n\n\t// setup global middleware\n\tr.Use(tgbot.Recover)\n\tr.Use(tgbot.Logger(os.Stdout))\n\n\t// modify path to be able to match user's commands via router\n\tr.Use(func(c *tgbot.Context) error {\n\t\tc.Path = c.Path + c.Text\n\t\treturn nil\n\t})\n\n\t// bind handler\n\tr.Bind(`^/message/(?:.*)/text/start(?:\\s(.*))?$`, func(c *tgbot.Context) error {\n\t\tlog.Println(c.Capture)             // - ^ from path\n\t\tlog.Println(c.Update.Message.Text) // or c.Text\n\n\t\t// send greeting message back\n\t\tmessage := \"hi there what's up\"\n\t\tresp, err := r.Messages.SendMessage(\n\t\t\tmessages.NewSendMessageParams().WithBody(\u0026models.SendMessageBody{\n\t\t\t\tText:   \u0026message,\n\t\t\t\tChatID: c.Update.Message.Chat.ID,\n\t\t\t}),\n\t\t)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif resp != nil {\n\t\t\tlog.Println(resp.Payload.Result.MessageID)\n\t\t}\n\t\treturn nil\n\t})\n\n\tif err := r.Poll(ctx, models.AllowedUpdateMessage); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\nDefault string representation of any kind of an update could be found here - [`router.go`](https://github.com/olebedev/go-tgbot/blob/master/router.go#L96-L226).\n\nRouter implements `http.Handler` interface to be able to serve HTTP as well. But, it's not recommended because webhooks are much much slower than polling.\n\nMore examples can be found at [godoc](https://godoc.org/github.com/olebedev/go-tgbot).\n\n---\n\nSee [the documentation](https://godoc.org/github.com/olebedev/go-tgbot) for more details.\n\n### LICENSE\n\nhttp://www.apache.org/licenses/LICENSE-2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folebedev%2Fgo-tgbot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Folebedev%2Fgo-tgbot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folebedev%2Fgo-tgbot/lists"}