英文:
how to use send_message() in python-telegram-bot
问题
我想要发送一条消息给用户,而不必等待用户触发我的机器人。使用send_message(),我已经阅读了文档,但我不太理解。以下是我的代码:
from dotenv import load_dotenv
from telegram.ext import *
from telegram import InputFile, Bot
import os
import re
command = ['start']
load_dotenv()
tokenize = os.getenv("TELEGRAM_BOT_TOKEN")
async def start_commmand(update, context):
umsg = update.message
await umsg.reply_text("欢迎,我将帮助您管理您的日程")
if __name__ == "__main__":
application = Application.builder().token(tokenize).build()
application.add_handler(CommandHandler(command[0], start_commmand))
# 运行机器人
application.run_polling(1.0)
我尝试使用send_message()向机器人用户发送消息,希望它可以发送一条消息,而不必等待用户的任何消息。因为在阅读了文档后,我不知道如何做到这一点。
英文:
i want to send a message to user, without waiting for user triggered my bot. using send_message(), i've been read the documentation but i'm not really understand. Here's my code
from dotenv import load_dotenv
from telegram.ext import *
from telegram import InputFile, Bot
import os
import re
command = ['start']
load_dotenv()
tokenize = os.getenv("TELEGRAM_BOT_TOKEN")
async def start_commmand(update, context):
umsg = update.message
await umsg.reply_text("Welcome, i'll help u with ur schedule")
if __name__ == "__main__":
application = Application.builder().token(tokenize).build()
application.add_handler(CommandHandler(command[0], start_commmand))
# Run bot
application.run_polling(1.0)
i tried to send a message to bot user using send_mesage(), i hope it's send a message without waiting any message from user. because i'm not really understand after read the documentation, i don't know how to do it
答案1
得分: 0
发送消息通常通过一个处理器进行,如果可以的话,你应该使用它们。
话虽如此,当然你可以发送消息,application
就像你的机器人。
举个例子,想象一下,如果你启动你的机器人,你想给自己发送一条消息,这可以通过添加一个 post_init
回调来实现,它将在调用 run_polling
时运行一次:
async def send_start_message(self):
await self.bot.sendMessage(CHAT_ID, 'Hello World')
if __name__ == "__main__":
application = Application.builder().token(TOKEN).build()
application.post_init = send_start_message
application.run_polling(1.0)
所以在这个例子中,'Hello World' 会在启动时发送到 CHAT_ID
一次。
英文:
Sending messages usually goes through a handler, you should use those if you can.
That said, of course you can send messages, the application
is sort of your bot.
As an example, imagine you want to send yourself a message if you start your bot, this can be done by adding a post_init
callback, that will run once when you call run_polling
:
async def send_start_message(self):
await self.bot.sendMessage(CHAT_ID, 'Hello World')
if __name__ == "__main__":
application = Application.builder().token(TOKEN).build()
application.post_init = send_start_message
application.run_polling(1.0)
So in this example 'Hello World' will be send to CHAT_ID
once, on start-up.
答案2
得分: 0
除了Ostone0的回答外,我想指出你根本不需要使用Application
类来发送消息。该类主要用于处理更新时才需要使用。如果只是要向机器人API发出普通调用,你可以直接使用Bot
类。这也在PTB wiki中有示例。
免责声明:我目前是python-telegram-bot
的维护者。
英文:
In addition to Ostone0s answer I'd like to point out that you don't need the Application
class at all to send a message. That class is intended to be used when you actually want to handle updates. For just making plain calls to the Bot API, you can directly use the Bot
class. This is also showcased in the PTB wiki.
Dicslaimer: I'm currently the maintainer of python-telegram-bot
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论