Skip to content
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

feat: Add Discord OAuth Flow and Bot Public Thread with History Response Feature #1129

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

AveryYay
Copy link
Collaborator

Description

This PR introduces 2 components:

  1. discord_OAuth_flow.py: Implements a Discord OAuth flow using FastAPI. This module allows users to authenticate with Discord, managing the entire OAuth process from generating the authorization URL, handling the authorization code exchange, to retrieving basic user information.
  2. discord_bot_public_thread_w_history.py: Defines a Discord bot that creates public threads in response to mentions. This bot collects and responds with recent message history (limit adjustable) from the channel, providing users with contextual information directly within the thread.

Motivation and Context

This change adds two distinct functionalities:

  1. OAuth Authentication - Enables secure user authentication via Discord OAuth, enhancing the bot’s integration with Discord’s authentication system.
  2. Public Thread with Message History - Introduces a feature for users to interact with the bot in public threads, where the bot responds with recent message history for better contextual understanding.
  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of example)

Implemented Tasks

  • Implemented OAuth flow for user authentication
  • Created a custom Discord bot with thread creation and history response capabilities
  • Added queuing for efficient message processing in the bot

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide. (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly. (required for a bug fix or a new feature)
  • I have updated the documentation accordingly.

…threads and responding with the chat history features
@koch3092
Copy link
Collaborator

Thanks @AveryYay , I will review the code and assist you in completing the PR integration

Copy link
Collaborator

@koch3092 koch3092 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AveryYay , good code! left some comments below.

And in terms of functionality, I hope you refer to camel.bots.slack.slack_app.py and integrate the oauth process into camel.bots.discord.discord_app.py (you can create camel.bots.discord to store module content)

examples/bots/discord_OAuth_flow.py Outdated Show resolved Hide resolved
examples/bots/discord_OAuth_flow.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@koch3092 koch3092 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @AveryYay , left some comments below.


user_data = await self.oauth_client.get_user_info(access_token)
logger.info(f"User data: {user_data}")
await self.shutdown_oauth_server()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await self.shutdown_oauth_server()

Server is a long-term service, so it's better to let users manually shut it down

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example can be combined with the usage of chat_agent to integrate existing historical records into the memory of chat_agent.

@Wendong-Fan Wendong-Fan changed the title Add Discord OAuth Flow and Bot Public Thread with History Response Feature feat: Add Discord OAuth Flow and Bot Public Thread with History Response Feature Nov 6, 2024
Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AveryYay ! Overall LGTM, left some comments below, It would be better if we could also add docstring for DiscordOAuth
BTW, you can run pre-commit run --all-files before the push to ensure the code pass pre-commit check

token: Optional[str] = None,
client_id: Optional[str] = None,
client_secret: Optional[str] = None,
redirect_uri: str = "http://localhost:8000/callback",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use REDIRECT_URI instead?

Comment on lines +58 to +59
response = await client.post(TOKEN_URL, data=data, headers=headers)
response_data = response.json()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before fetch the data, check the status code first like below?

if response.status_code != 200:
    logger.error("Failed to exchange code for token)
    return None

Comment on lines +65 to +66
user_response = await client.get(USER_URL, headers=headers)
return user_response.json()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Comment on lines +275 to +276
self.server.should_exit = True
logger.info("Shutting down the OAuth server.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add await here to make sure the server is fully stopped?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants