Telegram 电报可以通过id进行聊天,can';t by@name:未找到聊天记录
因此,我使用@BotFather制作了一个机器人,并通过我的帐户发送了消息。然后,我使用@userinfobot找到了我的帐户id,并发送了以下请求:Telegram 电报可以通过id进行聊天,can';t by@name:未找到聊天记录,telegram,telegram-bot,python-telegram-bot,Telegram,Telegram Bot,Python Telegram Bot,因此,我使用@BotFather制作了一个机器人,并通过我的帐户发送了消息。然后,我使用@userinfobot找到了我的帐户id,并发送了以下请求: https://api.telegram.org/botTOKEN/sendMessage?chat_id=id&text=test 它工作正常ok:true 但是,如果我尝试使用@username而不是id: https://api.telegram.org/botTOKENg/sendMessage?chat_id=@username&
https://api.telegram.org/botTOKEN/sendMessage?chat_id=id&text=test
它工作正常ok:true
但是,如果我尝试使用@username而不是id:
https://api.telegram.org/botTOKENg/sendMessage?chat_id=@username&text=test
我得到一个错误:
{
"ok": false,
"error_code": 400,
"description": "Bad Request: chat not found"
}
看看我过去的代码,我以前使用的
@
方法。事情变了吗?您必须使用id,不能使用用户名
将用户名作为参数仅适用于公共频道
请看一下:
说明目标频道的目标聊天或用户名的唯一标识符(格式为
@channelusername
)
您不能使用您应该使用的用户名发送消息,以便使用bot更新并从\u用户或有效\u用户处获取,然后访问id(聊天\u id)
- 您无法向从未使用过您的机器人的用户发送消息
import requests
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
def pure_api(chat_id, text):
# don't forget to set bot + TOKEN
r = requests.get(f"https://api.telegram.org/botTOKEN/sendMessage?chat_id={chat_id}&text={text}")
print(r.json())
def start(bot, update):
user_id = update.effective_user.id
print(user_id)
bot.send_message(user_id, "any message you want")
# or
update.message.reply_text("any message you want")
pure_api(user_id, "any message you want")
def main():
"""Start the bot."""
updater = Updater(TOKEN)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
我看了一下,它说你可以传递一个
@username
字符串。我在前面向这样的用户发送了消息。不,您在@parsecer中读错了“目标聊天的唯一标识符”或“目标聊天的唯一标识符”。