Not possible to use bot to create a topic in a private group with topic mode turned on.
When I try to create a new topic in a private group with topic mode enabled using the bot with the following code:
topicName := generateTopicName(message.Chat.Title)
iconColor := getRandomIconColor()
topicParams := &bot.CreateForumTopicParams{
ChatID: 12345678,
Name: topicName,
IconColor: iconColor,
}
topic, err := b.CreateForumTopic(ctx, topicParams)
if err != nil {
log.Printf("failed:%v", err)
errorMsg := &bot.SendMessageParams{
ChatID: message.Chat.ID,
Text: fmt.Sprintf("failed:%v", err),
}
b.SendMessage(ctx, errorMsg)
return
}
The program log returned: Bad request: chat not found
I have set the bot as an administrator of this group and allowed the bot to manage topics.
If the topic group is public, I can successfully create a new topic by using @groupname as the ChatID parameter. But even if the group is public, I cannot successfully create a topic using the group ID.
I don't think it's a library error.
Do you have a solution to this problem?
Its work fine for me
Warning, there is a recursion. Creates a topic on a message.
Testing on private group
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
opts := []bot.Option{
bot.WithDefaultHandler(handler),
}
b, err := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
if nil != err {
panic(err)
}
b.Start(ctx)
}
func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.Message == nil {
return
}
chatID := update.Message.Chat.ID
_, err := b.CreateForumTopic(ctx, &bot.CreateForumTopicParams{
ChatID: chatID,
Name: "Foo",
IconColor: 0,
IconCustomEmojiID: "",
})
if nil != err {
fmt.Printf("Error: %v\n", err)
}
}
By the way, how do you use update.Message.Chat.ID to represent the ID of a private topic group? I can only use numbers or usernames.
https://github.com/go-telegram/bot/blob/main/methods_params.go#L583
ChatID has any type
What I mean is how to use update.message to get the ID of the private topic group? Do I need to deliberately let the robot send a message in the group?
Another example
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
opts := []bot.Option{
bot.WithDefaultHandler(handler),
bot.WithMessageTextHandler("/add", bot.MatchTypeExact, addTopic),
}
b, _ := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)
b.Start(ctx)
}
func addTopic(ctx context.Context, b *bot.Bot, update *models.Update) {
b.CreateForumTopic(ctx, &bot.CreateForumTopicParams{
ChatID: update.Message.Chat.ID,
Name: "Foo topic",
IconColor: 0,
IconCustomEmojiID: "",
})
}
func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.MyChatMember == nil {
return
}
fmt.Printf("Chat ID: %d\n", update.MyChatMember.Chat.ID)
}
- Start the bot
- Add bot to the group
- Grant permissions (with can_manage_topics)
- Send
/add
You can store chatID in handler func and create forum topic from bot, without /add command, for example
I run this code:
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
opts := []bot.Option{
bot.WithDefaultHandler(handler),
bot.WithMessageTextHandler("/add", bot.MatchTypeExact, addTopic),
}
b, err := bot.New("TOKEN", opts...)
if err != nil {
log.Println(err)
log.Panicln(err.Error())
}
b.Start(ctx)
}
func addTopic(ctx context.Context, b *bot.Bot, update *models.Update) {
_, err := b.CreateForumTopic(ctx, &bot.CreateForumTopicParams{
ChatID: update.Message.Chat.ID,
Name: "Foo topic",
IconColor: 0,
IconCustomEmojiID: "",
})
if err != nil {
log.Println(err)
return
}
}
func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.MyChatMember == nil {
log.Println("ChatMember = nil")
return
}
fmt.Printf("Chat ID: %d\n", update.MyChatMember.Chat.ID)
}
-
The bot is already an administrator of the topic group
-
The bot has can_manage_topic permission
And I sent "/add" to the bot, The log prints:
Would you mind to record an instruction video to show how this work? I will be so gratitude.
Also, have you tried to use the numeric ID of the topic group, such as 12345678, as the parameter of ChatID to successfully create a topic?
@PiPiLuLuDoggy
Are you sure you are trying to do this in a Telegram group that is a “forum”? That is, it has topics.
Because, from the error text, I can see that the problem is precisely that you are trying to do this in a group that is not a forum.
Also, to verify this, I reproduced the issue. And yes, indeed, it does not work in a “non-forum”. But it works fine in a “forum” group. Which is to be expected.
@PiPiLuLuDoggy
You need to go to the “Manage Group” menu and enable ‘Topics’ in the corresponding section. This will make the group a “forum” and allow you to create topics, including using a bot. In a regular group, there are no topics, and a bot cannot simply create them.