forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_simple.py
More file actions
1148 lines (1010 loc) · 45.4 KB
/
Copy pathmain_simple.py
File metadata and controls
1148 lines (1010 loc) · 45.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from fastapi import FastAPI, Request, HTTPException, Query
from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse
import os
from dotenv import load_dotenv
from typing import List, Dict, Any
# Fix for Railway/production: Allow OAuth over HTTP (Railway handles HTTPS at proxy)
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
from simple_storage import SimpleUserStorage, SimpleSessionStorage, OAuthStateStorage, users, save_users
from twitter_client import TwitterClient
from tweet_detector import TweetDetector
load_dotenv()
# Initialize services
twitter_client = TwitterClient()
tweet_detector = TweetDetector()
app = FastAPI(
title="OMI Twitter Integration",
description="Real-time Twitter posting via OMI voice commands",
version="1.0.0"
)
@app.get("/")
async def root(uid: str = Query(None)):
"""Root endpoint with setup instructions."""
# If uid provided, show personalized setup page
if uid:
auth_url = f"/auth?uid={uid}"
return HTMLResponse(content=f"""
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
margin: 0;
padding: 20px;
background: linear-gradient(135deg, #1DA1F2 0%, #0d8bd9 100%);
min-height: 100vh;
}}
.container {{
max-width: 600px;
margin: 0 auto;
background: white;
border-radius: 16px;
padding: 30px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}}
h1 {{
color: #1DA1F2;
margin-top: 0;
font-size: 28px;
}}
.icon {{
font-size: 48px;
margin-bottom: 20px;
}}
.step {{
background: #f8f9fa;
padding: 15px;
border-radius: 8px;
margin: 15px 0;
border-left: 4px solid #1DA1F2;
}}
.step h3 {{
margin-top: 0;
color: #1DA1F2;
font-size: 16px;
}}
.example {{
background: #e8f5fe;
padding: 10px;
border-radius: 6px;
margin: 10px 0;
font-style: italic;
}}
.btn {{
display: inline-block;
background: #1DA1F2;
color: white;
padding: 15px 30px;
border-radius: 30px;
text-decoration: none;
font-weight: bold;
margin: 20px 0;
transition: background 0.3s;
}}
.btn:hover {{
background: #0d8bd9;
}}
ul {{
line-height: 1.8;
}}
</style>
</head>
<body>
<div class="container">
<div class="icon">🐦✨</div>
<h1>Twitter Voice Poster</h1>
<p>Tweet with your voice using OMI!</p>
<a href="{auth_url}" class="btn">🔐 Connect Twitter Account</a>
<div class="step">
<h3>📱 How to Use</h3>
<p>After connecting your Twitter account:</p>
<ol>
<li>Say <strong>"Tweet Now"</strong> to your OMI device</li>
<li>Speak your tweet naturally</li>
<li>AI automatically detects when you're done</li>
<li>Your tweet is posted instantly! 🚀</li>
</ol>
</div>
<div class="step">
<h3>💬 Examples</h3>
<div class="example">
"Tweet Now, Just had an amazing conversation about AI and creativity!"
</div>
<div class="example">
"Tweet Now, Beautiful sunset today. This is incredible. End tweet."
</div>
<div class="example">
"Post Tweet, Excited to share my new project with the world!"
</div>
</div>
<div class="step">
<h3>🎯 Trigger Phrases</h3>
<ul>
<li>"Tweet Now"</li>
<li>"Post Tweet"</li>
<li>"Send Tweet"</li>
<li>"Tweet This"</li>
</ul>
</div>
<div class="step">
<h3>💡 Pro Tips</h3>
<ul>
<li>Speak naturally - AI cleans up filler words</li>
<li>Say "End tweet" to finish explicitly</li>
<li>Natural pauses are detected automatically</li>
<li>Works best with 1-2 sentence tweets</li>
</ul>
</div>
<p style="text-align: center; color: #666; margin-top: 30px;">
Made with ❤️ for the OMI community
</p>
</div>
</body>
</html>
""")
# Default API info
return {
"app": "OMI Twitter Integration",
"version": "1.0.0",
"status": "active",
"storage": "in-memory (simple mode)",
"endpoints": {
"auth": "/auth?uid=<user_id>",
"webhook": "/webhook?session_id=<session>&uid=<user_id>",
"setup_check": "/setup-completed?uid=<user_id>"
}
}
@app.get("/auth")
async def auth_start(uid: str = Query(..., description="User ID from OMI")):
"""Start OAuth flow for Twitter authentication."""
redirect_uri = os.getenv("OAUTH_REDIRECT_URL", "http://localhost:8000/auth/callback")
try:
# Get authorization URL (Tweepy generates its own state parameter)
# We store the mapping between Tweepy's state and our uid internally
auth_url = twitter_client.get_authorization_url(redirect_uri, uid)
# Don't modify the URL - Tweepy's state parameter is already included
return RedirectResponse(url=auth_url)
except Exception as e:
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=f"OAuth initialization failed: {str(e)}")
@app.get("/auth/callback")
async def auth_callback(
request: Request,
state: str = Query(None),
code: str = Query(None)
):
"""Handle OAuth callback from Twitter."""
if not code:
return HTMLResponse(
content="""
<html>
<body style="font-family: Arial; padding: 40px; text-align: center;">
<h2>❌ Authentication Failed</h2>
<p>Authorization code not received. Please try again.</p>
</body>
</html>
""",
status_code=400
)
# state is Tweepy's generated state parameter
try:
# Exchange code for access token using stored OAuth handler
# This also retrieves the uid we associated with this state
full_url = str(request.url)
token_data, uid = twitter_client.get_access_token(full_url, state)
# Save user tokens with expiration info
access_token = token_data.get('access_token')
refresh_token = token_data.get('refresh_token')
expires_in = token_data.get('expires_in', 7200)
print(f"🔑 Token data received:", flush=True)
print(f" Access token: {access_token[:20]}..." if access_token else " Access token: None", flush=True)
print(f" Refresh token: {refresh_token[:20]}..." if refresh_token else " Refresh token: None", flush=True)
print(f" Expires in: {expires_in}s ({expires_in/3600:.1f}h)", flush=True)
SimpleUserStorage.save_user(
uid=uid,
access_token=access_token,
refresh_token=refresh_token,
expires_in=expires_in
)
return HTMLResponse(
content="""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Twitter • Account Connected</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background: #FFFFFF;
min-height: 100vh;
display: flex;
flex-direction: column;
}
.header {
padding: 16px 20px;
border-bottom: 1px solid #EFF3F4;
display: flex;
align-items: center;
justify-content: center;
}
.twitter-logo {
width: 32px;
height: 32px;
fill: #1D9BF0;
}
.container {
flex: 1;
max-width: 600px;
width: 100%;
margin: 0 auto;
padding: 32px 20px;
}
.success-card {
background: #FFFFFF;
border: 1px solid #EFF3F4;
border-radius: 16px;
padding: 32px 24px;
text-align: center;
margin-bottom: 16px;
}
.check-icon {
width: 64px;
height: 64px;
background: #00BA7C;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 20px;
}
.check-icon svg {
width: 32px;
height: 32px;
stroke: white;
fill: none;
stroke-width: 3;
}
h1 {
font-size: 31px;
font-weight: 800;
color: #0F1419;
margin-bottom: 8px;
line-height: 1.2;
}
.subtitle {
font-size: 17px;
color: #536471;
margin-bottom: 20px;
font-weight: 400;
}
.info-card {
background: #FFFFFF;
border: 1px solid #EFF3F4;
border-radius: 16px;
padding: 20px;
text-align: left;
margin-bottom: 16px;
}
.info-title {
font-size: 20px;
font-weight: 700;
color: #0F1419;
margin-bottom: 16px;
}
.step {
display: flex;
gap: 12px;
margin-bottom: 16px;
padding-bottom: 16px;
border-bottom: 1px solid #EFF3F4;
}
.step:last-child {
border-bottom: none;
margin-bottom: 0;
padding-bottom: 0;
}
.step-icon {
font-size: 24px;
flex-shrink: 0;
}
.step-content {
flex: 1;
}
.step-text {
color: #0F1419;
font-size: 15px;
line-height: 1.5;
font-weight: 400;
}
.step-text strong {
font-weight: 700;
color: #1D9BF0;
}
.example-card {
background: #F7F9F9;
border: 1px solid #EFF3F4;
border-radius: 16px;
padding: 20px;
margin-bottom: 16px;
}
.example-label {
font-size: 13px;
font-weight: 700;
color: #536471;
margin-bottom: 12px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.tweet-example {
background: white;
border: 1px solid #CFD9DE;
border-radius: 12px;
padding: 16px;
}
.tweet-text {
color: #0F1419;
font-size: 15px;
line-height: 1.5;
}
.tweet-meta {
display: flex;
align-items: center;
gap: 8px;
margin-top: 12px;
font-size: 13px;
color: #536471;
}
.divider {
height: 1px;
background: #EFF3F4;
margin: 24px 0;
}
.footer {
text-align: center;
padding: 20px;
font-size: 13px;
color: #536471;
}
.footer a {
color: #1D9BF0;
text-decoration: none;
}
@media (max-width: 600px) {
.container {
padding: 24px 16px;
}
.success-card,
.info-card,
.example-card {
padding: 24px 16px;
}
h1 {
font-size: 27px;
}
.subtitle {
font-size: 15px;
}
}
</style>
</head>
<body>
<div class="header">
<svg class="twitter-logo" viewBox="0 0 24 24" aria-hidden="true">
<g><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"></path></g>
</svg>
</div>
<div class="container">
<div class="success-card">
<div class="check-icon">
<svg viewBox="0 0 24 24">
<polyline points="20 6 9 17 4 12"></polyline>
</svg>
</div>
<h1>You're connected!</h1>
</div>
<div class="info-card">
<div class="info-title">How to use</div>
<div class="step">
<div class="step-icon">🎤</div>
<div class="step-content">
<div class="step-text">Say <strong>"Tweet Now"</strong> to your OMI device</div>
</div>
</div>
<div class="step">
<div class="step-icon">💬</div>
<div class="step-content">
<div class="step-text">Speak your message naturally</div>
</div>
</div>
<div class="step">
<div class="step-icon">✨</div>
<div class="step-content">
<div class="step-text">AI cleans up your speech and posts it</div>
</div>
</div>
<div class="step">
<div class="step-icon">📲</div>
<div class="step-content">
<div class="step-text">Get notified when your tweet is live!</div>
</div>
</div>
</div>
<div class="example-card">
<div class="example-label">Try saying</div>
<div class="tweet-example">
<div class="tweet-text">Tweet Now, I just had an incredible idea about voice-first social media!</div>
<div class="tweet-meta">
<span>🤖</span>
<span>AI will clean and post this automatically</span>
</div>
</div>
</div>
</div>
<div class="footer">
<p>Powered by <a href="https://omi.me" target="_blank">OMI</a> • Made with ❤️</p>
</div>
</body>
</html>
"""
)
except Exception as e:
error_uid = state if state else "unknown"
return HTMLResponse(
content=f"""
<html>
<body style="font-family: Arial; padding: 40px; text-align: center;">
<h2>❌ Authentication Error</h2>
<p>Failed to complete authentication: {str(e)}</p>
<p><a href="/auth?uid={error_uid}">Try again</a></p>
</body>
</html>
""",
status_code=500
)
@app.get("/setup-completed")
async def check_setup(uid: str = Query(..., description="User ID from OMI")):
"""Check if user has completed setup (authenticated with Twitter)."""
is_setup = SimpleUserStorage.is_authenticated(uid)
return {"is_setup_completed": is_setup}
@app.post("/webhook")
async def webhook(
request: Request,
uid: str = Query(..., description="User ID from OMI"),
session_id: str = Query(None, description="Session ID from OMI (optional)"),
sample_rate: int = Query(None, description="Sample rate (optional, for audio streams)")
):
"""
Real-time transcript webhook endpoint.
Handles requests with or without session_id parameter.
"""
# Use consistent session_id per user (no timestamp!)
# This ensures session persists across all segments
if not session_id:
session_id = f"omi_session_{uid}"
# Get user
user = SimpleUserStorage.get_user(uid)
if not user or not user.get("access_token"):
return JSONResponse(
content={
"message": "User not authenticated. Please complete setup first.",
"setup_required": True
},
status_code=401
)
# Check if token needs refresh
if SimpleUserStorage.is_token_expired(uid):
print(f"🔄 Token expired for user {uid[:10]}...", flush=True)
# Check if we have a valid refresh token
refresh_token = user.get("refresh_token")
if not refresh_token or refresh_token == "null":
print(f"⚠️ No refresh token! User must re-authenticate with offline.access scope.", flush=True)
return JSONResponse(
content={
"message": "🔄 Your session expired. Please re-authenticate in the OMI app to continue tweeting.",
"setup_required": True
},
status_code=401
)
# Try to refresh
try:
print(f"🔄 Refreshing token...", flush=True)
new_token_data = twitter_client.refresh_access_token(refresh_token)
# Save new tokens
SimpleUserStorage.save_user(
uid=uid,
access_token=new_token_data.get("access_token"),
refresh_token=new_token_data.get("refresh_token", refresh_token),
expires_in=new_token_data.get("expires_in", 7200)
)
# Update user reference
user = SimpleUserStorage.get_user(uid)
print(f"✅ Token refreshed!", flush=True)
except Exception as e:
print(f"❌ Refresh error: {e}", flush=True)
# Delete old invalid token
del users[uid]
save_users()
return JSONResponse(
content={
"message": "🔄 Session expired. Please re-authenticate in the OMI app.",
"setup_required": True
},
status_code=401
)
# Parse payload from OMI
try:
payload = await request.json()
except Exception as e:
raise HTTPException(status_code=400, detail=f"Invalid JSON payload: {str(e)}")
# Handle both formats:
# 1. Direct list: [{"text": "...", ...}, ...]
# 2. Dict with segments: {"session_id": "...", "segments": [...]}
segments = []
if isinstance(payload, dict):
# Extract segments from dict
segments = payload.get("segments", [])
# Use session_id from payload if not in query params
if not session_id and "session_id" in payload:
session_id = payload["session_id"]
elif isinstance(payload, list):
# Direct list of segments
segments = payload
# Log what we received for debugging
print(f"📥 Received {len(segments) if segments else 0} segment(s) from OMI", flush=True)
if segments:
for i, seg in enumerate(segments[:3]): # Show first 3
text = seg.get('text', 'NO TEXT') if isinstance(seg, dict) else str(seg)
print(f" Segment {i}: {text[:100]}", flush=True)
if not segments or not isinstance(segments, list):
# Silent response for empty/invalid data
return {"status": "ok"}
# Ensure we have a consistent session_id per user
# Use uid as session_id so it persists across calls
if not session_id:
session_id = f"omi_session_{uid}"
# Get or create session
session = SimpleSessionStorage.get_or_create_session(session_id, uid)
# Debug: show current session state
print(f"📊 Session state: mode={session.get('tweet_mode')}, count={session.get('segments_count', 0)}", flush=True)
# Process segments
response_message = await process_segments(session, segments, user)
# Only send notifications for final tweet post (success or failure)
# Silent responses during collection so user doesn't get spammed
if response_message and ("✅ Tweet posted:" in response_message or "❌ Failed:" in response_message):
print(f"✉️ USER NOTIFICATION: {response_message}", flush=True)
return {
"message": response_message,
"session_id": session_id,
"processed_segments": len(segments)
}
# Silent response for everything else (listening, collecting, etc.)
print(f"🔇 Silent response: {response_message}", flush=True)
return {"status": "ok"}
async def process_segments(
session: dict,
segments: List[Dict[str, Any]],
user: dict
) -> str:
"""
ALWAYS collect exactly 3 segments after 'Tweet Now', then AI extracts the tweet.
- Segment 1: Contains "Tweet Now" + start of tweet
- Segment 2: Middle part (auto-collected)
- Segment 3: End part (auto-collected)
- AI decides what's actually the tweet and cleans it
"""
# Extract text from segments
segment_texts = [seg.get("text", "") for seg in segments]
full_text = " ".join(segment_texts)
session_id = session["session_id"]
print(f"🔍 Received: '{full_text}'", flush=True)
print(f"📊 Session mode: {session['tweet_mode']}, Count: {session.get('segments_count', 0)}/3", flush=True)
# Check for trigger phrase
if tweet_detector.detect_trigger(full_text):
tweet_content = tweet_detector.extract_tweet_content(full_text)
print(f"🎤 TRIGGER! Starting 3-segment collection...", flush=True)
print(f" Segment 1 content: '{tweet_content}'", flush=True)
# Start collecting - ALWAYS wait for 2 more segments
SimpleSessionStorage.update_session(
session_id,
tweet_mode="recording",
accumulated_text=tweet_content,
segments_count=1
)
# Silent - don't notify user yet
return "collecting_1"
# If in recording mode, collect more segments
elif session["tweet_mode"] == "recording":
accumulated = session.get("accumulated_text", "")
segments_count = session.get("segments_count", 0)
# Add this segment
accumulated += " " + full_text
segments_count += 1
print(f"📝 Segment {segments_count}/3: '{full_text}'", flush=True)
print(f"📚 Full accumulated: '{accumulated[:150]}...'", flush=True)
# Always collect 3 segments
if segments_count >= 3:
print(f"✅ Got all 3 segments! Sending to AI...", flush=True)
# AI extracts the actual tweet from all 3 segments
cleaned_content = await tweet_detector.ai_extract_tweet_from_segments(accumulated)
print(f"✨ AI extracted tweet: '{cleaned_content}'", flush=True)
if len(cleaned_content.strip()) > 3:
print(f"📤 Posting to Twitter...", flush=True)
result = await twitter_client.post_tweet(user["access_token"], cleaned_content)
if result and result.get("success"):
SimpleSessionStorage.reset_session(session_id)
print(f"🎉 SUCCESS! Tweet ID: {result.get('tweet_id')}", flush=True)
return f"✅ Tweet posted: '{cleaned_content}'"
else:
error = result.get("error", "Unknown") if result else "Failed"
SimpleSessionStorage.reset_session(session_id)
print(f"❌ FAILED: {error}", flush=True)
return f"❌ Failed: {error}"
else:
SimpleSessionStorage.reset_session(session_id)
print(f"⚠️ AI returned empty tweet", flush=True)
return "❌ No valid tweet content"
else:
# Still collecting (need segment 2 or 3)
SimpleSessionStorage.update_session(
session_id,
accumulated_text=accumulated,
segments_count=segments_count
)
# Silent - don't notify user yet
return f"collecting_{segments_count}"
# Passive listening - silent
return "listening"
@app.get("/test")
async def test_interface():
"""Web interface for testing tweet functionality."""
return HTMLResponse(content="""
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Twitter Voice Poster - Test Interface</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
margin: 0;
padding: 20px;
background: #f5f8fa;
}
.container {
max-width: 800px;
margin: 0 auto;
}
.header {
background: linear-gradient(135deg, #1DA1F2 0%, #0d8bd9 100%);
color: white;
padding: 30px;
border-radius: 12px;
margin-bottom: 20px;
}
.card {
background: white;
border-radius: 12px;
padding: 25px;
margin-bottom: 20px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #14171a;
}
input[type="text"], textarea {
width: 100%;
padding: 12px;
border: 2px solid #e1e8ed;
border-radius: 8px;
font-size: 16px;
box-sizing: border-box;
font-family: inherit;
}
input[type="text"]:focus, textarea:focus {
outline: none;
border-color: #1DA1F2;
}
textarea {
min-height: 100px;
resize: vertical;
}
.btn {
background: #1DA1F2;
color: white;
padding: 12px 24px;
border: none;
border-radius: 30px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
margin-right: 10px;
transition: background 0.3s;
}
.btn:hover {
background: #0d8bd9;
}
.btn:disabled {
background: #aab8c2;
cursor: not-allowed;
}
.btn-secondary {
background: #657786;
}
.btn-secondary:hover {
background: #4a5a6a;
}
.status {
padding: 15px;
border-radius: 8px;
margin: 15px 0;
font-weight: 500;
}
.status.idle {
background: #e8f5fe;
color: #1DA1F2;
}
.status.recording {
background: #fff3cd;
color: #856404;
}
.status.success {
background: #d4edda;
color: #155724;
}
.status.error {
background: #f8d7da;
color: #721c24;
}
.log {
background: #f7f9fa;
border: 1px solid #e1e8ed;
border-radius: 8px;
padding: 15px;
max-height: 300px;
overflow-y: auto;
font-family: 'Monaco', 'Courier New', monospace;
font-size: 13px;
}
.log-entry {
padding: 5px 0;
border-bottom: 1px solid #e1e8ed;
}
.log-entry:last-child {
border-bottom: none;
}
.timestamp {
color: #657786;
margin-right: 10px;
}
.example {
background: #f0f8ff;
padding: 10px;
border-radius: 6px;
margin: 5px 0;
font-size: 14px;
cursor: pointer;
border: 2px solid transparent;
}
.example:hover {
border-color: #1DA1F2;
}
.auth-status {
display: inline-block;
padding: 6px 12px;
border-radius: 20px;
font-size: 14px;
font-weight: 600;
}
.auth-status.connected {
background: #d4edda;
color: #155724;
}
.auth-status.disconnected {
background: #f8d7da;
color: #721c24;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🐦 Twitter Voice Poster - Test Interface</h1>
<p>Test your voice commands without using OMI device</p>
<div>
<span class="auth-status" id="authStatus">Checking...</span>
</div>
</div>
<div class="card">
<h2>Authentication</h2>
<div class="input-group">
<label for="uid">User ID (UID):</label>
<input type="text" id="uid" placeholder="Enter your OMI user ID" value="test_user_123">
</div>
<button class="btn" onclick="authenticate()">🔐 Authenticate Twitter</button>
<button class="btn btn-secondary" onclick="checkAuth()">🔍 Check Auth Status</button>
</div>
<div class="card">
<h2>Test Voice Commands</h2>
<div class="input-group">
<label for="voiceInput">What would you say to OMI:</label>
<textarea id="voiceInput" placeholder='Example: "Tweet Now, Just had an amazing idea about AI and creativity!"'></textarea>
</div>
<button class="btn" onclick="sendCommand()">🎤 Send Command</button>
<button class="btn btn-secondary" onclick="clearLogs()">🗑️ Clear Logs</button>
<div id="status" class="status idle">
Status: Ready
</div>
</div>
<div class="card">
<h3>Quick Examples (Click to use)</h3>
<div class="example" onclick="useExample(this)">
Tweet Now, Just launched my new AI project and it's incredible!
</div>
<div class="example" onclick="useExample(this)">
Tweet Now, Beautiful sunset today. Nature never stops amazing me. End tweet.
</div>
<div class="example" onclick="useExample(this)">
Post Tweet, Excited to share my thoughts on the future of voice interfaces!
</div>
<div class="example" onclick="useExample(this)">
Tweet Now, Sometimes the best ideas come when you least expect them.
</div>
</div>
<div class="card">
<h2>Activity Log</h2>
<div id="log" class="log">
<div class="log-entry">
<span class="timestamp">Ready</span>
<span>Waiting for commands...</span>
</div>
</div>
</div>
</div>
<script>
const sessionId = 'test_session_' + Date.now();
function addLog(message, type = 'info') {
const log = document.getElementById('log');
const entry = document.createElement('div');
entry.className = 'log-entry';
const time = new Date().toLocaleTimeString();
entry.innerHTML = `<span class="timestamp">[${time}]</span><span>${message}</span>`;
log.insertBefore(entry, log.firstChild);
}