Update telegram_bot.py
Browse files- telegram_bot.py +55 -9
telegram_bot.py
CHANGED
|
@@ -4,7 +4,7 @@ import logging
|
|
| 4 |
import asyncio
|
| 5 |
from datetime import date
|
| 6 |
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
| 7 |
-
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes
|
| 8 |
|
| 9 |
# Import your existing functions from your app file
|
| 10 |
# Note: Ensure your app.py functions return strings/data instead of using st.write
|
|
@@ -29,17 +29,63 @@ def get_creds():
|
|
| 29 |
return authenticate_service() # Ensure this function works without st.session_state
|
| 30 |
|
| 31 |
# --- BOT HANDLERS ---
|
|
|
|
|
|
|
| 32 |
|
| 33 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
async def inbox_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 45 |
"""Combines Agent 1 (Fetch) and Agent 2 (Summarize)"""
|
|
|
|
| 4 |
import asyncio
|
| 5 |
from datetime import date
|
| 6 |
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
| 7 |
+
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes, ConversationHandler
|
| 8 |
|
| 9 |
# Import your existing functions from your app file
|
| 10 |
# Note: Ensure your app.py functions return strings/data instead of using st.write
|
|
|
|
| 29 |
return authenticate_service() # Ensure this function works without st.session_state
|
| 30 |
|
| 31 |
# --- BOT HANDLERS ---
|
| 32 |
+
# States
|
| 33 |
+
EMAIL, AUTH_CODE = range(2)
|
| 34 |
|
| 35 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 36 |
+
await update.message.reply_text("π Welcome! To get started, please enter your **Gmail Address**:")
|
| 37 |
+
return EMAIL
|
| 38 |
+
|
| 39 |
+
async def get_email(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 40 |
+
user_email = update.message.text
|
| 41 |
+
context.user_data['email'] = user_email
|
| 42 |
+
|
| 43 |
+
# Generate Auth URL
|
| 44 |
+
flow = get_auth_flow()
|
| 45 |
+
auth_url, _ = flow.authorization_url(prompt='consent', access_type='offline')
|
| 46 |
+
|
| 47 |
+
await update.message.reply_text(
|
| 48 |
+
f"Great! Now I need permission to access {user_email}.\n\n"
|
| 49 |
+
f"1. [Click here to Login]({auth_url})\n"
|
| 50 |
+
f"2. Authorize the app.\n"
|
| 51 |
+
f"3. You will be redirected to a page (it might look like an error or localhost).\n"
|
| 52 |
+
f"4. **Copy the URL** of that page and paste it here.",
|
| 53 |
+
parse_mode='Markdown'
|
| 54 |
)
|
| 55 |
+
return AUTH_CODE
|
| 56 |
+
|
| 57 |
+
async def get_code(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 58 |
+
raw_text = update.message.text
|
| 59 |
+
# Extract code from URL if they pasted the whole link
|
| 60 |
+
if "code=" in raw_text:
|
| 61 |
+
code = raw_text.split("code=")[1].split("&")[0]
|
| 62 |
+
else:
|
| 63 |
+
code = raw_text
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
flow = get_auth_flow()
|
| 67 |
+
flow.fetch_token(code=code)
|
| 68 |
+
creds = flow.credentials
|
| 69 |
+
|
| 70 |
+
# Save creds in memory (or a database/file for persistence)
|
| 71 |
+
context.user_data['creds'] = creds
|
| 72 |
+
|
| 73 |
+
await update.message.reply_text("β
Authentication successful! You can now use /inbox and /schedule.")
|
| 74 |
+
return ConversationHandler.END
|
| 75 |
+
except Exception as e:
|
| 76 |
+
await update.message.reply_text(f"β Auth failed: {str(e)}. Please try /start again.")
|
| 77 |
+
return ConversationHandler.END
|
| 78 |
+
|
| 79 |
+
# Add to your main block:
|
| 80 |
+
auth_conv = ConversationHandler(
|
| 81 |
+
entry_points=[CommandHandler('start', start)],
|
| 82 |
+
states={
|
| 83 |
+
EMAIL: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_email)],
|
| 84 |
+
AUTH_CODE: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_code)],
|
| 85 |
+
},
|
| 86 |
+
fallbacks=[CommandHandler('start', start)],
|
| 87 |
+
)
|
| 88 |
+
application.add_handler(auth_conv)
|
| 89 |
|
| 90 |
async def inbox_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 91 |
"""Combines Agent 1 (Fetch) and Agent 2 (Summarize)"""
|