Skip to content

Commit 23bbaa5

Browse files
committed
fix(twitter): prompt user for username if login failed with emails
1 parent 07578a1 commit 23bbaa5

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

npi/browser_app/twitter/app.py

+39-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
22
import os
33
import re
4+
5+
from npiai_proto import api_pb2
46
from playwright.async_api import TimeoutError
57
from markdownify import MarkdownConverter
68

@@ -9,6 +11,8 @@
911
from npi.browser_app.navigator import Navigator
1012
from npi.config import config
1113
from npi.error.auth import UnauthorizedError
14+
from npi.core.callback import callback
15+
from npi.core.thread import Thread
1216
from .schema import *
1317

1418
__SYSTEM_PROMPT__ = """
@@ -45,8 +49,6 @@
4549
'home': 'https://twitter.com/home'
4650
}
4751

48-
from ...core.thread import Thread
49-
5052

5153
class ImageFilterConverter(MarkdownConverter):
5254
def process_text(self, el):
@@ -122,9 +124,9 @@ def __init__(self, llm=None, headless: bool = True):
122124
async def start(self, thread: Thread = None):
123125
if not self._started:
124126
await super().start(thread)
125-
await self._login()
127+
await self._login(thread)
126128

127-
async def _login(self):
129+
async def _login(self, thread: Thread):
128130
if os.path.exists(self.state_file):
129131
with open(self.state_file, 'r') as f:
130132
state = json.load(f)
@@ -143,6 +145,19 @@ async def _login(self):
143145
await self.playwright.page.get_by_test_id('loginButton').click()
144146
await self.playwright.page.get_by_label('Phone, email, or username').fill(self.creds.username)
145147
await self.playwright.page.get_by_role('button', name='Next').click()
148+
149+
# check if username(not email) is required
150+
await self.playwright.page.wait_for_timeout(1000)
151+
username_input = self.playwright.page.get_by_test_id("ocfEnterTextTextInput")
152+
if await username_input.count() != 0:
153+
username = await self._request_username(thread)
154+
await username_input.fill(username)
155+
await self.playwright.page.get_by_test_id("ocfEnterTextNextButton").click()
156+
157+
await self.playwright.page.wait_for_timeout(1000)
158+
if await username_input.count() != 0:
159+
raise UnauthorizedError('Unable to login to Twitter. Please try again with the correct credentials.')
160+
146161
await self.playwright.page.get_by_label('Password', exact=True).fill(self.creds.password)
147162
await self.playwright.page.get_by_test_id('LoginForm_Login_Button').click()
148163
await self.playwright.page.wait_for_url(__ROUTES__['home'])
@@ -152,6 +167,26 @@ async def _login(self):
152167
os.makedirs(save_dir, exist_ok=True)
153168
await self.playwright.context.storage_state(path=self.state_file)
154169

170+
await thread.send_msg(callback.Callable('Logged in to Twitter'))
171+
172+
@staticmethod
173+
async def _request_username(thread: Thread) -> str:
174+
if thread is None:
175+
raise Exception('`thread` must be provided to request username')
176+
177+
cb = callback.Callable(
178+
action=api_pb2.ActionResponse(
179+
type=api_pb2.ActionType.HUMAN_FEEDBACK,
180+
human_feedback=api_pb2.HumanFeedbackAction(
181+
type=api_pb2.HumanFeedbackActionType.INPUT,
182+
notice='Please enter your username (not email) to continue the login process.',
183+
)
184+
),
185+
)
186+
cb.action.action_id = cb.id()
187+
await thread.send_msg(cb=cb)
188+
return await cb.wait()
189+
155190
@npi_tool
156191
async def get_current_page(self):
157192
"""Get the title and url of the current page."""

0 commit comments

Comments
 (0)