-
Notifications
You must be signed in to change notification settings - Fork 0
Implement telegram integration for task creation and event extraction, plus saving to db #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from app.models.task_input import TaskInput | ||
|
|
||
| __all__ = ["TaskInput"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from datetime import datetime | ||
| from sqlalchemy import DateTime, Integer, String, Text, JSON | ||
| from sqlalchemy.orm import Mapped, mapped_column | ||
|
|
||
| from app.database import Base | ||
|
|
||
|
|
||
| class TaskInput(Base): | ||
| __tablename__ = "task_inputs" | ||
|
|
||
| id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) | ||
|
|
||
| source: Mapped[str] = mapped_column(String(50), nullable=False) | ||
| original_text: Mapped[str] = mapped_column(Text, nullable=False) | ||
|
|
||
| extracted_event: Mapped[dict] = mapped_column(JSON, nullable=False) | ||
|
|
||
| telegram_chat_id: Mapped[str | None] = mapped_column(String(100), nullable=True) | ||
| telegram_message_id: Mapped[str | None] = mapped_column(String(100), nullable=True) | ||
|
|
||
| created_at: Mapped[datetime] = mapped_column( | ||
| DateTime, | ||
| default=datetime.utcnow, | ||
| nullable=False, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from app.models.task_input import TaskInput | ||
|
|
||
|
|
||
| async def save_task_input( | ||
| db: AsyncSession, | ||
| source: str, | ||
| original_text: str, | ||
| extracted_event: dict, | ||
| telegram_chat_id: str | None = None, | ||
| telegram_message_id: str | None = None, | ||
| ) -> TaskInput: | ||
|
|
||
| task_input = TaskInput( | ||
| source=source, | ||
| original_text=original_text, | ||
| extracted_event=extracted_event, | ||
| telegram_chat_id=telegram_chat_id, | ||
| telegram_message_id=telegram_message_id, | ||
| ) | ||
|
|
||
| db.add(task_input) | ||
|
|
||
| await db.commit() | ||
| await db.refresh(task_input) | ||
|
|
||
| return task_input |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import os | ||
| import logging | ||
|
|
||
| from dotenv import load_dotenv | ||
| from telegram import Update | ||
| from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, ContextTypes, filters | ||
| from app.services.event_extractor import extract_event_from_text | ||
| from app.services.openrouter_client import OpenRouterError | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from app.database import AsyncSessionLocal | ||
| from app.services.task_input_service import save_task_input | ||
|
|
||
| load_dotenv() | ||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | ||
| await update.message.reply_text( | ||
| "Hi! Send me a task and I’ll turn it into a TaskCraft event draft." | ||
| ) | ||
|
|
||
|
|
||
| async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | ||
| message_text = update.message.text | ||
| timezone = "Asia/Jerusalem" | ||
|
|
||
| await update.message.reply_text("Processing task... ⏳") | ||
|
|
||
| try: | ||
| event = await extract_event_from_text(message_text, timezone) | ||
|
|
||
| async with AsyncSessionLocal() as db: | ||
| await save_task_input( | ||
| db=db, | ||
| source="telegram", | ||
| original_text=message_text, | ||
| extracted_event=event, | ||
| telegram_chat_id=str(update.effective_chat.id), | ||
| telegram_message_id=str(update.message.message_id), | ||
| ) | ||
| response = f""" | ||
| ✅ Event Draft Created | ||
|
|
||
| 📌 Title: {event["title"]} | ||
| 🕒 Start: {event["start_at"]} | ||
| 🕔 End: {event["end_at"]} | ||
| 📅 All day: {event["all_day"]} | ||
| 🎯 Confidence: {event["confidence"]} | ||
|
|
||
| 📝 Notes: | ||
| {event["notes"]} | ||
| """.strip() | ||
|
|
||
| if event["missing_info"]: | ||
| response += "\n\n❓ Missing info:\n" + "\n".join( | ||
| f"- {item}" for item in event["missing_info"] | ||
| ) | ||
|
|
||
| await update.message.reply_text(response) | ||
|
|
||
| except OpenRouterError as e: | ||
| await update.message.reply_text(f"❌ OpenRouter failed:\n{str(e)}") | ||
|
|
||
| except Exception as e: | ||
| await update.message.reply_text(f"❌ Something went wrong:\n{str(e)}") | ||
|
|
||
| def run_bot() -> None: | ||
| token = os.getenv("TELEGRAM_BOT_TOKEN") | ||
|
|
||
| if not token: | ||
| raise RuntimeError("Missing TELEGRAM_BOT_TOKEN in .env") | ||
|
|
||
| app = ApplicationBuilder().token(token).build() | ||
|
|
||
| app.add_handler(CommandHandler("start", start)) | ||
| app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) | ||
|
|
||
| logger.info("Starting Telegram bot with polling...") | ||
| app.run_polling() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| run_bot() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Missing telegram dependency
🐞 Bug≡ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools