Hi , anyone tried to communicate with a telegram bot using streamlit?
Here is my code :
#telegram_send.send(messages=["Wow that was easy!"])
#connecting and building the session
client = TelegramClient('test_session', api_id, api_hash)**
client.connect()
#in case of script ran first time it will
#ask either to input token or otp sent to
#number or sent or your telegram id
if not client.is_user_authorized():
client.send_code_request(phone)
#signing in the client
input_code = st.text_input('Enter the code')
client.sign_in(phone, input_code)
try:
#receiver user_id and access_hash, use
#my user_id and access_hash for reference
receiver = InputPeerUser(user_id, user_hash)
#sending message using telegram client
client.send_message(receiver, message, parse_mode='html')
except Exception as e:
#there may be many error coming in while like peer
#error, wrong access_hash, flood_error, etc
print(e);
#disconnecting the telegram session
client.disconnect()
The error I am having is
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'ScriptRunner.scriptThread'.
If this helps, here’s an example where I define an asyncio loop at the start of the app if none exists and store it in session_state for future usage. I think this is also how streamit-webrtc manages it
import asyncio
def get_or_create_eventloop():
try:
return asyncio.get_event_loop()
except RuntimeError as ex:
if "There is no current event loop in thread" in str(ex):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return asyncio.get_event_loop()
if "loop" not in st.session_state:
st.session_state.loop = asyncio.new_event_loop()
asyncio.set_event_loop(st.session_state.loop)
# ... do the Telegram stuff
now I’m wondering if that can be put into experimental_singleton instead but … I doubt it
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.