File size: 6,696 Bytes
52c83ed
 
 
 
 
7ab10b5
500f63b
52c83ed
2c5c44e
52c83ed
 
7ab10b5
2c5c44e
52c83ed
 
 
 
7ab10b5
 
52c83ed
 
ae0eb3d
 
52c83ed
7ab10b5
52c83ed
7ab10b5
500f63b
 
 
 
 
 
 
7ab10b5
500f63b
 
 
 
2c5c44e
500f63b
 
 
2c5c44e
7ab10b5
500f63b
 
7ab10b5
500f63b
 
52c83ed
7ab10b5
52c83ed
7ab10b5
52c83ed
 
 
 
2c5c44e
52c83ed
7ab10b5
52c83ed
 
 
 
 
 
 
 
2c5c44e
52c83ed
7ab10b5
 
 
 
52c83ed
 
7ab10b5
52c83ed
 
 
7ab10b5
 
 
52c83ed
2c5c44e
52c83ed
 
2c5c44e
52c83ed
 
2c5c44e
52c83ed
 
9c34178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52c83ed
9c34178
52c83ed
9c34178
 
 
 
 
 
 
 
 
 
7ab10b5
 
 
 
 
 
 
 
 
9c34178
59a21d6
 
 
 
 
 
9c34178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import logging
import asyncio
from datetime import date
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes, ConversationHandler

# Import from app.py
from app import (
    fetch_unread_emails, summarize_email, draft_reply_email,
    parse_schedule_prompt, create_calendar_event,
    build, CHATGPT_MODEL, client, get_auth_flow
)

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

# --- AUTH CONVERSATION STATES ---
EMAIL, AUTH_CODE = range(2)

# --- HELPERS ---
def get_creds(context):
    return context.user_data.get('creds')

# --- HANDLER FUNCTIONS ---
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text("πŸ‘‹ Welcome! Please enter your **Gmail Address**:")
    return EMAIL

async def get_email(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user_email = update.message.text
    context.user_data['email'] = user_email
    flow = get_auth_flow()
    auth_url, _ = flow.authorization_url(prompt='consent', access_type='offline')
    await update.message.reply_text(f"Login here: [Google Login]({auth_url})\nPaste the code/URL below.", parse_mode='Markdown')
    return AUTH_CODE

async def get_code(update: Update, context: ContextTypes.DEFAULT_TYPE):
    raw_text = update.message.text
    code = raw_text.split("code=")[1].split("&")[0] if "code=" in raw_text else raw_text
    try:
        flow = get_auth_flow()
        flow.fetch_token(code=code)
        context.user_data['creds'] = flow.credentials
        await update.message.reply_text("βœ… Auth successful! Try /inbox.")
        return ConversationHandler.END
    except Exception as e:
        await update.message.reply_text(f"❌ Auth failed: {str(e)}")
        return ConversationHandler.END

async def inbox_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
    creds = get_creds(context)
    if not creds:
        await update.message.reply_text("❌ Please /start first.")
        return
    service = build("gmail", "v1", credentials=creds)
    emails = fetch_unread_emails(service)
    if not emails:
        await update.message.reply_text("πŸ“­ No unread emails.")
        return
    summary_text = "🎯 *Summaries:*\n\n"
    for email in emails:
        summary = summarize_email(email['body'], email['from'], email['subject'])
        summary_text += f"{summary}\n\n"
        context.user_data[email['from'].lower()] = email
    await update.message.reply_text(summary_text, parse_mode='Markdown')

async def reply_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
    if not context.args:
        await update.message.reply_text("Usage: `/reply Name`")
        return
    target = " ".join(context.args).lower()
    found = next((v for k, v in context.user_data.items() if target in str(k)), None)
    if found:
        reply = draft_reply_email(found['subject'], found['body'], "AI Assistant")
        await update.message.reply_text(f"πŸ“ *Draft:* \n\n`{reply}`", parse_mode='Markdown')
    else:
        await update.message.reply_text("❌ Sender not found.")

async def schedule_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user_prompt = " ".join(context.args)
    creds = get_creds(context)
    if not user_prompt or not creds:
        await update.message.reply_text("Usage: `/schedule meeting at 2pm` (and ensure you are logged in)")
        return
    json_res = parse_schedule_prompt(user_prompt, date.today(), "UTC")
    try:
        event_data = json.loads(json_res)
        result = create_calendar_event(creds, event_data, "UTC")
        await update.message.reply_text(result, parse_mode='Markdown')
    except Exception as e:
        await update.message.reply_text(f"❌ Error: {str(e)}")

# --- MAIN RUNNER ---
# if __name__ == '__main__':
#     token = os.environ.get("TELEGRAM_BOT_TOKEN")
    
#     # 1. Define the ConversationHandler AFTER the functions are defined
#     auth_conv = ConversationHandler(
#         entry_points=[CommandHandler('start', start)],
#         states={
#             EMAIL: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_email)],
#             AUTH_CODE: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_code)],
#         },
#         fallbacks=[CommandHandler('start', start)],
#     )

#     # 2. Build application
#     application = ApplicationBuilder().token(token).build()
    
#     # 3. Add handlers
#     application.add_handler(auth_conv)
#     application.add_handler(CommandHandler('inbox', inbox_handler))
#     application.add_handler(CommandHandler('reply', reply_handler))
#     application.add_handler(CommandHandler('schedule', schedule_handler))

#     print("βœ… Bot is live and polling...")
#     application.run_polling(drop_pending_updates=True)



# --- At the bottom of telegram_bot.py ---

if __name__ == '__main__':
    import time
    
    token = os.environ.get("TELEGRAM_BOT_TOKEN")
    if not token:
        print("❌ CRITICAL ERROR: TELEGRAM_BOT_TOKEN is missing from environment variables.")
        exit(1)

    # 1. Wait for the container network to actually wake up
    print("⏳ Waiting 10 seconds for network stability...")
    time.sleep(10)

    # 2. Define handlers
    auth_conv = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            EMAIL: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_email)],
            AUTH_CODE: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_code)],
        },
        fallbacks=[CommandHandler('start', start)],
    )

    # 3. Initialize Application
    application = ApplicationBuilder().token(token).build()
    application.add_handler(auth_conv)
    application.add_handler(CommandHandler('inbox', inbox_handler))
    application.add_handler(CommandHandler('reply', reply_handler))
    application.add_handler(CommandHandler('schedule', schedule_handler))

    # 4. Start with a Retry Loop
    print("πŸš€ Attempting to connect to Telegram...")
    
    max_retries = 5
    for attempt in range(max_retries):
        try:
            print(f"πŸ”„ Connection attempt {attempt + 1}/{max_retries}...")
            application.run_polling(drop_pending_updates=True)
            break # If it succeeds, break the loop
        except Exception as e:
            print(f"⚠️ Connection failed: {e}")
            if attempt < max_retries - 1:
                print("😴 Waiting 15 seconds before retrying...")
                time.sleep(15)
            else:
                print("❌ All connection attempts failed. Please check Hugging Face network settings.")