|
Hi, ok so i might sound like dumb when i say this, but i cannot run the bot, the readme file is kinda hard to understand for me i don't why i never had these kind of problems, i need someone to explain exactly how i can run the bot with full function, and again sorry for the dumb question |
Replies: 1 comment 1 reply
|
Hi, no issues, you always need to start somewhere and you don't need to know everything from start, so this is fine First I would like to suggest you reading How do bots work, Telegram Bot FAQ for basic questions, then about BotFather. Once you understand basic concepts and building blocks for creating bots, try to create bot using BotFather, getting it's token and running very simple bots, like from README example. Also there is official tutorial for Java, but it still can be useful. main.go package main
import (
"context"
"fmt"
"os"
"github.com/mymmrac/telego"
)
func main() {
// Get Bot token from environment variables
botToken := os.Getenv("TOKEN")
// Create bot and enable debugging info
// Note: Please keep in mind that default logger may expose sensitive information,
// use in development only
// (more on configuration in examples/configuration/main.go)
bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Call method getMe (https://core.telegram.org/bots/api#getme)
botUser, err := bot.GetMe(context.Background())
if err != nil {
fmt.Println("Error:", err)
}
// Print Bot information
fmt.Printf("Bot user: %+v\n", botUser)
}To run this, you need token, provide it via environment variables, you can do this like that: TOKEN=YOUR_BOT_TOKEN_GOES_HERE go run main.goAs a result, you should see something like this: Once you have this working, you can try processing updates (user interactions), again example from README package main
import (
"context"
"fmt"
"os"
"github.com/mymmrac/telego"
)
func main() {
botToken := os.Getenv("TOKEN")
// Note: Please keep in mind that default logger may expose sensitive information,
// use in development only
bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Get updates channel
// (more on configuration in examples/updates_long_polling/main.go)
updates, _ := bot.UpdatesViaLongPolling(context.Background(), nil)
// Loop through all updates when they came
for update := range updates {
fmt.Printf("Update: %+v\n", update)
}
}Once you run this and text your bot, you should see messages come to your terminal. From this you just go with logic your bot should do, like repling with messages, or keyboards, or images, whatever you desire _, err := bot.SendMessage(ctx,
tu.Message(
tu.ID(chatID),
update.Message.Text,
),
)But don't forget to handle errors, this is very important step that is omitted from examples to save time and show more about Telego and not basic error handling. |
Hi, no issues, you always need to start somewhere and you don't need to know everything from start, so this is fine
First I would like to suggest you reading How do bots work, Telegram Bot FAQ for basic questions, then about BotFather. Once you understand basic concepts and building blocks for creating bots, try to create bot using BotFather, getting it's token and running very simple bots, like from README example. Also there is official tutorial for Java, but it still can be useful.
main.go