R
R
ricch_scrim_prod2021-01-30 21:17:10
Python
ricch_scrim_prod, 2021-01-30 21:17:10

How to send messages to yourself through a telegram bot?

How can I send a message to myself with a certain text through a bot?
I use this function:

def send():
    Bot.send_message(chat_id=MY_ID, text='hello')
send()

Gives an error message. I use aiogram. I'm a newbie so don't be too harsh

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shurshur, 2021-01-31
@ricch_scrim_prod

A mistake, in fact, it is necessary to show, who will decide what, I don’t know what?
But let me povang.
First, Bot is a class. You need to create an instance of it:

from aiogram import Bot
bot = Bot(token=config.bot_token)

Secondly, this will still result in the coroutine was never awaited error. Because asynchronous functions cannot just be called from synchronous code.
You can call like this:
async def main():
  await bot.send_message(my_chat_id, my_message)
  await bot.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

A
Andrew, 2021-01-30
@Mikyc

Hello, I'm not really into telegram bots either, but try this: Or message.chat.id Should work . That is, here the bot receives the user id (your) and sends a message. PS if it doesn't come out hard, don't hit)
Bot.send_message(message.from_user.id, "hello")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question