Using python and visual studio, how do I intercept a toast notification on Windows, copy the text from it, and then paste it in desired location?

56 views Asked by At

I am trying to create a program (an automated process) that logs into a certain website, which prompts a Duo Push (authentication method) that sends a text message to my phone. I need to copy this text message (which is prompted as a toast notification on Windows 10 (attached) and then paste it into the Duo Push text space and then click the verify button. Though I know how to do virtually everything in this process, I am struggling with the means of catching the windows toast notification on my Windows computer, taking the SMS code from it, and then storing it in the clipboard (and then pasting it).

It seems as though every library in python (the programming language I am using to create this) does not have this function (e.g., win10toast) to intercept windows toast notifications, let alone read it. If it helps I am using selenium and chrome web driver to do this web process.

If you can please give me any insight as to if this can be done, I would much appreciate it.

Below is the code in question (I am using winsdk/asyncio/pyperclip)

       # Create a notification listener
       listener = UserNotificationListener.current()
       # Request access to the notifications
       accessStatus = await listener.request_access_async() # This is a valid await expression
       # Check if access is allowed
       if accessStatus != UserNotificationListenerAccessStatus.ALLOWED:
           # Print an error message and quit the browser
           print("Access to UserNotificationListener is not allowed.")
           browser.quit()
       # Define a handler function for notification changes
       def handler(listener, event):
           # Get the notification object
           notification = listener.get_notification(event.user_notification_id)
           # Check if the notification is a text message
           if notification.app_info.display_info.display_name == "Messaging":
               # Get the text elements from the notification
               text_sequence = notification.notification.visual.get_binding(KnownNotificationBindings.get_toast_generic()).get_text_elements()
               # Get the message text from the second element
               message_text = text_sequence[1].text
               # Check if the message contains the SMS passcode format
               if "SMS passcode (will expire in 10 minutes:" in message_text:
                   # Extract the passcode from the message
                   passcode = message_text.split(":")[1].strip()
                   # Copy the passcode to the clipboard
                   pyperclip.copy(passcode)
                   # Print a confirmation message
                   print("SMS passcode copied to clipboard.")
                   # Find the verification box element by its id and paste the code there
                   verification_box = wait.until(EC.presence_of_element_located((By.ID, "passcode")))
                   verification_box.send_keys(pyperclip.paste())
                   # Find the submit button element by its name and click it
                   submit_button = wait.until(EC.element_to_be_clickable((By.NAME, "_eventId_proceed")))
                   submit_button.click()
       # Add the handler function to the notification listener
       listener.add_notification_changed(handler)

I unfortunately get this error, and I do not know what to do from here:

File "C:\Users\Me\source\repos\PythonApplication1\TestingPleaseWorkDearLord.py", line 51, in login
    listener = UserNotificationListener.current()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '_winsdk_Windows_UI_Notifications_Management.UserNotificationListener' object is not callable

Thank you for any help you may provide.

0

There are 0 answers