如何使用 telethon 作为用户机器人将消息发送到电报中的特定主题[关闭]

问题描述 投票:0回答:1

我是一个拥有各种主题的超级群组的管理员。超级团体有各种各样的主题。其中之一的标题是:“测试”。发送到该主题的第一条消息的消息链接是:https://t.me/c/211XXXXXXX/16/17

我使用 API ID 和哈希作为用户机器人登录 telethon。现在,我想使用 telethon 向该主题发送一条消息。 我向 -100211XXXXXXX 发送了消息,该消息向 #general 发送了消息,而不是我想要的主题。

我如何向该主题/小组/论坛发送简单的消息?

python telegram telegram-bot telethon
1个回答
0
投票

如果您是管理员,那么您有权获取您所在组的访问令牌。

如果您遇到机器人令牌问题,可以查看这篇文章:https://helpdesk.bitrix24.com/open/17622486/

替换下面代码中的机器人令牌和聊天 ID,

import requests

bottoken = 'BOT_TOKEN'
chat_id = '211XXXXXXX'
message = 'Hi!'

根据上述信息创建以下 URL,

url = f'https://api.telegram.org/bot{bottoken}/sendMessage'

# Set the parameters
params = {
    'chat_id': chat_id,
    'text': message
}

在这里,您可以查看已发送消息的回复:

resp = requests.post(url, params=params)

# Check the response
if resp.status_code == 200:
    print('Message sent successfully!')
else:
    print('Failed to send message:', response.text)

如果代码不起作用,请在评论中告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.