bot icon indicating copy to clipboard operation
bot copied to clipboard

Not possible to use bot to create a topic in a private group with topic mode turned on.

Open PiPiLuLuDoggy opened this issue 1 year ago • 13 comments

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.

PiPiLuLuDoggy avatar Nov 24 '24 15:11 PiPiLuLuDoggy

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.

PiPiLuLuDoggy avatar Nov 24 '24 15:11 PiPiLuLuDoggy

I don't think it's a library error.

negasus avatar Nov 25 '24 09:11 negasus

Do you have a solution to this problem?

PiPiLuLuDoggy avatar Nov 25 '24 11:11 PiPiLuLuDoggy

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)
	}
}

negasus avatar Nov 25 '24 13:11 negasus

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.

PiPiLuLuDoggy avatar Nov 25 '24 16:11 PiPiLuLuDoggy

https://github.com/go-telegram/bot/blob/main/methods_params.go#L583 ChatID has any type

negasus avatar Nov 25 '24 16:11 negasus

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?

PiPiLuLuDoggy avatar Nov 25 '24 16:11 PiPiLuLuDoggy

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)
}
  1. Start the bot
  2. Add bot to the group
  3. Grant permissions (with can_manage_topics)
  4. Send /add

negasus avatar Nov 26 '24 07:11 negasus

You can store chatID in handler func and create forum topic from bot, without /add command, for example

negasus avatar Nov 26 '24 07:11 negasus

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)
}
  1. The bot is already an administrator of the topic group

  2. The bot has can_manage_topic permission

And I sent "/add" to the bot, The log prints: image

Would you mind to record an instruction video to show how this work? I will be so gratitude.

PiPiLuLuDoggy avatar Nov 26 '24 09:11 PiPiLuLuDoggy

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 avatar Nov 26 '24 09:11 PiPiLuLuDoggy

@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.

Image Image

steindvart avatar Oct 30 '25 18:10 steindvart

@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.

Image

steindvart avatar Oct 30 '25 18:10 steindvart