aiogram - 如何执行功能?
aiogram - How to execute a function?
更新:
经过几次无休止的解释问题后,完全重写了问题:
如何在启动时执行功能?
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = 'API'
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
@dp.message_handler()
async def echo(message: types.Message):
await message.answer(message.text)
async def notify_message() # THIS FUNCTION
# await bot.sendMessage(chat.id, 'Bot Started')
await print('Hello World')
if __name__ == '__main__':
notifty_message() # doesn't work
executor.start_polling(dp, skip_updates=True)
试过没有成功::
if __name__ == '__main__':
dp.loop.create_task(notify_message()) # Function to execute
executor.start_polling(dp, skip_updates=True)
AttributeError: 'NoneType' 对象没有属性 'create_task'
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.create_task(notify_message()) # Function to execute
executor.start_polling(dp, skip_updates=True)
TypeError: 对象 NoneType 不能在 'await' 表达式中使用
如果你想发起对话,那是不可能的,电报不允许机器人发送第一条消息。但是如果你想保存你的用户 ID,你可以创建一个数据库或者只是一个 txt 文件并在其中写入任何新的用户 ID。您将能够通过从数据库输入 id 而不是 chat.id
来向所有这些人发送消息
我给你一个例子,说明如何在不使用 message.answer 的情况下发送消息,只需使用:
@dp.message_handler()
async def echo(message: types.Message):
await bot.send_message(message.chat.id, message.text)
我不明白你为什么需要打印 ('The bot is running') 当 运行 "aiogram" 它告诉你机器人已经启动了
2021-06-01 09:31:42,729:INFO:Bot: YourBot [@YourBot]
2021-06-01 09:31:42,729:WARNING:Updates were skipped successfully.
2021-06-01 09:31:42,729:INFO:Start polling.
比想象中简单多了。 捂脸
工作解决方案:
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = 'API'
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
@dp.message_handler()
async def echo(message: types.Message):
await bot.send_message(message.chat.id, message.text)
def test_hi():
print("Hello World")
if __name__ == '__main__':
test_hi()
executor.start_polling(dp, skip_updates=True)
第二种方法是正确的,但是asyncio事件循环没有初始化。所以,如果在机器人旁边调用异步函数很重要,你会这样做:
from asyncio import get_event_loop
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = 'API'
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot=bot, loop=get_event_loop()) # Initialising event loop for the dispatcher
async def notify_message():
print('Hello World')
if __name__ == '__main__':
dp.loop.create_task(notify_message()) # Providing awaitable as an argument
executor.start_polling(dp, skip_updates=True)
你可以调用 aiogram.executor.start(dispatcher, future)
到 运行 异步函数(未来)
from aiogram import Bot, Dispatcher, executor
bot = Bot(token='your_api_token')
dp = Dispatcher(bot)
async def notify_message() # THIS FUNCTION
# await bot.sendMessage(chat.id, 'Bot Started')
await print('Hello World')
@dp.message_handler()
async def echo(message: types.Message):
await message.answer(message.text)
if __name__ == '__main__':
executor.start(dp, notify_message())
executor.start_polling(dp, skip_updates=True)
更新: 经过几次无休止的解释问题后,完全重写了问题:
如何在启动时执行功能?
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = 'API'
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
@dp.message_handler()
async def echo(message: types.Message):
await message.answer(message.text)
async def notify_message() # THIS FUNCTION
# await bot.sendMessage(chat.id, 'Bot Started')
await print('Hello World')
if __name__ == '__main__':
notifty_message() # doesn't work
executor.start_polling(dp, skip_updates=True)
试过没有成功::
if __name__ == '__main__':
dp.loop.create_task(notify_message()) # Function to execute
executor.start_polling(dp, skip_updates=True)
AttributeError: 'NoneType' 对象没有属性 'create_task'
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.create_task(notify_message()) # Function to execute
executor.start_polling(dp, skip_updates=True)
TypeError: 对象 NoneType 不能在 'await' 表达式中使用
如果你想发起对话,那是不可能的,电报不允许机器人发送第一条消息。但是如果你想保存你的用户 ID,你可以创建一个数据库或者只是一个 txt 文件并在其中写入任何新的用户 ID。您将能够通过从数据库输入 id 而不是 chat.id
我给你一个例子,说明如何在不使用 message.answer 的情况下发送消息,只需使用:
@dp.message_handler()
async def echo(message: types.Message):
await bot.send_message(message.chat.id, message.text)
我不明白你为什么需要打印 ('The bot is running') 当 运行 "aiogram" 它告诉你机器人已经启动了
2021-06-01 09:31:42,729:INFO:Bot: YourBot [@YourBot]
2021-06-01 09:31:42,729:WARNING:Updates were skipped successfully.
2021-06-01 09:31:42,729:INFO:Start polling.
比想象中简单多了。 捂脸
工作解决方案:
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = 'API'
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
@dp.message_handler()
async def echo(message: types.Message):
await bot.send_message(message.chat.id, message.text)
def test_hi():
print("Hello World")
if __name__ == '__main__':
test_hi()
executor.start_polling(dp, skip_updates=True)
第二种方法是正确的,但是asyncio事件循环没有初始化。所以,如果在机器人旁边调用异步函数很重要,你会这样做:
from asyncio import get_event_loop
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = 'API'
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot=bot, loop=get_event_loop()) # Initialising event loop for the dispatcher
async def notify_message():
print('Hello World')
if __name__ == '__main__':
dp.loop.create_task(notify_message()) # Providing awaitable as an argument
executor.start_polling(dp, skip_updates=True)
你可以调用 aiogram.executor.start(dispatcher, future)
到 运行 异步函数(未来)
from aiogram import Bot, Dispatcher, executor
bot = Bot(token='your_api_token')
dp = Dispatcher(bot)
async def notify_message() # THIS FUNCTION
# await bot.sendMessage(chat.id, 'Bot Started')
await print('Hello World')
@dp.message_handler()
async def echo(message: types.Message):
await message.answer(message.text)
if __name__ == '__main__':
executor.start(dp, notify_message())
executor.start_polling(dp, skip_updates=True)