Issue
I'm trying to create a small script in Python to get Telegram notification and then take action based on message.
I'm struggling to create a loop that listen Telegram and return parsed text I need.
import re
import asyncio
from telethon import TelegramClient, events
def extract_trigger(new_message):
return re.findall(r'\$(.+?)\b', new_message)
#@client.on(events.NewMessage(chats=user_input_channel))
async def new_message_listener(event):
#Get Message Text
new_message = event
print(extract_coin(str(new_message)))
if str(new_message).find(message_filter) != -1 :
trigger = extract_trigger(str(new_message))
print('Found! ' + trigger[0])
await client.disconnect()
return trigger[0]
async def main():
coro = await new_message_listener(events.NewMessage(chats=user_input_channel))
trigger = coro
print(trigger)
api_id = xxxxxx
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = TelegramClient('session_read', api_id, api_hash)
user_input_channel = 'https://t.me/mychan'
message_filter = 'Trigger $'
client.start()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
I'm quite new with Python and have some difficulties with async function. At this point, the script loop and only print "none" variable in the main function.
If I use this small script I'm able to get the message but it's not "looping" on the listener.
from telethon import TelegramClient, events
api_id = xxxxxxx
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = TelegramClient('session_read', api_id, api_hash)
user_input_channel = 'https://t.me/mychan'
message_filter = 'Trigger $'
client.start()
@client.on(events.NewMessage(chats=user_input_channel))
async def newMessageListener(event):
#Get Message Text
newMessage = event.message.message
if newMessage.find(message_filter) != -1 :
print('Found! ' + newMessage)
else:
print('Not found!')
with client:
client.run_until_disconnected()
can someone help me with this ?
Solution
The second snippet is correct, but it insists on running the event loop for you using run_until_disconnected
. This is just a convenience API, though, and you can instead run the event loop yourself. Your top-level function can fire up the bot and wait for the event callback to provide it with the information it needs, e.g. using an asyncio.Future
:
from telethon import TelegramClient, events
api_id = xxxxxxx
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
async def get_msg_async():
client = TelegramClient('session_read', api_id, api_hash)
user_input_channel = 'https://t.me/mychan'
message_filter = 'Trigger $'
found = asyncio.get_event_loop().create_future()
@client.on(events.NewMessage(chats=user_input_channel))
async def newMessageListener(event):
newMessage = event.message.message
if newMessage.find(message_filter) != -1 :
print('Found! ' + newMessage)
found.set_result(newMessage)
else:
print('Not found!')
# start the client
await client.start()
# allow the client to run in the background and wait for
# newMessageListener to provide the message
return await found
def get_msg():
return asyncio.run(get_msg_async())
# now in your sync code you should be able to use:
msg = get_msg()
Answered By - user4815162342
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.