Skip to content

Instantly share code, notes, and snippets.

@chainyo
Created January 9, 2023 19:01
Show Gist options
  • Select an option

  • Save chainyo/0c5d2fdb72108dc1d0ef05aed4188e48 to your computer and use it in GitHub Desktop.

Select an option

Save chainyo/0c5d2fdb72108dc1d0ef05aed4188e48 to your computer and use it in GitHub Desktop.

Revisions

  1. chainyo created this gist Jan 9, 2023.
    78 changes: 78 additions & 0 deletions send_summary.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    async def send_summary_as_dm(
    self,
    guild: discord.Guild,
    user: discord.User,
    summary_size: str,
    timeframe: str,
    language: str,
    job_name: str,
    token: str,
    summarized_chat: Optional[List[str]] = None,
    ) -> None:
    """
    Send summary as DM.
    Parameters
    ----------
    guild: discord.Guild
    The guild the user is in.
    user: discord.User
    The user to send the summary to.
    summary_size: str
    The summary size used to generate the summary.
    timeframe: str
    The timeframe used to generate the summary.
    language: str
    The language used to generate the summary.
    job_name: str
    The job name.
    token: str
    The Wordcab API token.
    summarized_chat: Optional[List[str]]
    The summarized chat to send if the user requested it.
    """
    while True:
    job = retrieve_job(job_name=job_name, api_key=token)
    status = job.job_status
    if status == "SummaryComplete":
    break
    elif status == "Deleted" or status == "Error":
    await user.send(f"Your job has been [{status}]. Please try again.")
    await asyncio.sleep(3)

    summary_id = job.summary_details["summary_id"]
    await bot_db.store_summary_id(summary_id=summary_id, discord_guild_id=guild.id)
    summary = retrieve_summary(summary_id=summary_id, api_key=token)
    await user.send(f"**Your summary:**")
    for utterance in summary.summary[summary_size]["structured_summary"]:
    await user.send(f"```{utterance.summary}```")

    if summarized_chat is not None:
    await user.send("**Chats used for the summary:**")
    # Send the summarized chat in chunks of 2000 characters
    joined_chat: str = ""
    for chat in summarized_chat:
    if len(joined_chat) + len(chat) > 2000:
    await user.send(f"```{joined_chat}```")
    joined_chat = ""
    joined_chat += f"\n{chat}"
    await user.send(f"```{joined_chat}```")

    include_chat = True if summarized_chat is not None else False
    time_started = datetime.strptime(summary.time_started, "%Y-%m-%dT%H:%M:%S.%fZ")
    time_completed = datetime.strptime(summary.time_completed, "%Y-%m-%dT%H:%M:%S.%fZ")
    response_time = (time_completed - time_started).total_seconds()
    self.usage_tracking.log_metrics(
    user=user.name,
    guild_name=guild.name,
    summary_size=summary_size,
    timeframe=timeframe,
    language=language,
    include_chat=include_chat,
    time_started=time_started,
    time_completed=time_completed,
    response_time=response_time,
    )

    # Delete job and users data after summary is sent
    await self.delete_job_after_summary(job_name=job_name, token=token)