OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to send a telegram message without blocking main thread

  • Thread starter Thread starter Appaji Chintimi
  • Start date Start date
A

Appaji Chintimi

Guest
Code:
from telegram import Bot
import asyncio

TOKEN = "blah"
CHAT_ID = 1

async def send_message_async():
    message = "This is a test update from the bot!"
    bot = Bot(token=TOKEN)
    await bot.send_message(chat_id=CHAT_ID, text=message)
    print("Message sent successfully!")


async def main():

    print("Before sending message...")

    await send_message_async()

    print("After sending message...") # I don't want this to be blocked until the message is sent


if __name__ == "__main__":
    asyncio.run(main())

I'm trying to send message without blocking whole thread, but I'm struggling to avoid it. Is there any way?

I am hoping to find a simpler API to above problem. Something like below

Code:
def send_message():
    # Sends message to user asynchonously without blocking the main thread
    pass

def main():
    # Main function
    send_message()

But it seems like, to call one async function, every other function should be converted into async function.
<pre class="lang-py prettyprint-override"><code>from telegram import Bot
import asyncio

TOKEN = "blah"
CHAT_ID = 1

async def send_message_async():
message = "This is a test update from the bot!"
bot = Bot(token=TOKEN)
await bot.send_message(chat_id=CHAT_ID, text=message)
print("Message sent successfully!")


async def main():

print("Before sending message...")

await send_message_async()

print("After sending message...") # I don't want this to be blocked until the message is sent


if __name__ == "__main__":
asyncio.run(main())
</code></pre>
<p>I'm trying to send message without blocking whole thread, but I'm struggling to avoid it. Is there any way?</p>
<p>I am hoping to find a simpler API to above problem. Something like below</p>
<pre class="lang-py prettyprint-override"><code>def send_message():
# Sends message to user asynchonously without blocking the main thread
pass

def main():
# Main function
send_message()
</code></pre>
<p>But it seems like, to call one async function, every other function should be converted into async function.</p>
 

Latest posts

ن
Replies
0
Views
1
نعمان منذر محمود الجميلي
ن
Top