Package: agentcomms
Connectors for your agent to the outside world.Discord connector with (voice and chat, DMs coming)Twitter connector (feed only, DMs coming)Admin Panel - simple web interface to chat with your agent and upload filesInstallationpipinstallagentcommsTwitter Usage GuideThis module uses a set of environment variables to interact with Twitter, so you'll need to set the following before using:TWITTER_EMAIL: The email address for your Twitter account.TWITTER_USERNAME: The username for your Twitter account.TWITTER_PASSWORD: The password for your Twitter account.Setting up the Twitter connectorBefore you can start using the Twitter connector, you have to initialize it. The initialization is done using thestart_twitter_connectorfunction.importtwittertwitter.start_twitter_connector()This will start the twitter connector with default parameters. If you wish to customize the email, username, password, or session storage path, you can use thestart_twitterfunction like so:twitter.start_twitter(email="my_email@example.com",username="my_username",password="my_password",session_storage_path="my_session.cookies")Liking a tweetTo like a tweet, you can use thelike_tweetfunction. Pass in the id of the tweet you wish to like.twitter.like_tweet("1234567890")Replying to a tweetTo reply to a tweet, you can use thereply_to_tweetfunction. Pass in the message you wish to send, and the id of the tweet you're replying to.twitter.reply_to_tweet("This is a great tweet!","1234567890")Posting a tweetTo post a new tweet, you can use thetweetfunction. Pass in the message you wish to tweet. You can optionally pass in a media object to attach to the tweet.twitter.tweet("Hello, Twitter!")Registering feed handlersFeed handlers are functions that get called whenever there are new tweets in the feed. They can be registered using theregister_feed_handlerfunction.defmy_feed_handler(tweet):print(f"New tweet from{tweet['user']['name']}:{tweet['text']}")twitter.register_feed_handler(my_feed_handler)You can also unregister a handler using theunregister_feed_handlerfunction.twitter.unregister_feed_handler(my_feed_handler)Getting account informationTo get the current account object, you can use theget_accountfunction.account=twitter.get_account()print(account.username)This will print out the username of the current Twitter account.Discord Usage GuideThe Discord connector works with both voice and text. For voice, you will need an Elevenlabs API key.Environment VariablesBefore you start, you need to set the environment variables for the bot to function correctly. Create a.envfile in your project directory and set these variables:DISCORD_API_TOKEN=your_discord_bot_token
ELEVENLABS_API_KEY=your_elevenlabs_api_key
ELEVENLABS_VOICE=voice_you_want_to_use
ELEVENLABS_MODEL=model_you_want_to_useDISCORD_API_TOKENis your Discord bot token, which you get when you create a new bot on the Discord developer portal.ELEVENLABS_API_KEYis your Eleven Labs API key for their TTS service.ELEVENLABS_VOICEis the voice you want to use for the TTS. You will have to check the Eleven Labs API documentation for the voices they support.ELEVENLABS_MODELis the TTS model you want to use. Again, you will have to check the Eleven Labs API documentation for the supported models.Running the BotAfter setting your environment variables, you can run your bot by calling thestart_connectorfunction:start_connector()Registering Message HandlersMessage handlers are functions that are executed when certain events happen in Discord, such as receiving a message. Here's how you can register a message handler:Create the Handler FunctionFirst, you need to create a function that will be executed when a message is received. This function should take one argument, which is the message that was received. The message object will contain all the information about the message, such as the content of the message, the author, and the channel where it was sent.Here's an example of a simple message handler function:defhandle_message(message):print(f"Received a message from{message.author}:{message.content}")This function will simply print the author and content of every message that is received.Register the Handler FunctionTo register the handler function, you use theregister_feed_handlerfunction and pass the handler function as an argument:register_feed_handler(handle_message)After calling this function, thehandle_messagefunction will be executed every time a message is received on Discord.Public Functionssend_message(message: str, channel_id: int)This function is used to add a message to the queue. The message will be sent to the channel with the ID specified.send_message("Hello world!",1234567890)start_connector(discord_api_token: str)This function is used to start the bot and the event loop, setting the bot to listen for events on Discord.start_connector("your_discord_api_token")register_feed_handler(func: callable)This function is used to register a new function as a feed handler. Feed handlers are functions that process or respond to incoming data in some way.defmy_func(data):print(data)register_feed_handler(my_func)unregister_feed_handler(func: callable)This function is used to remove a function from the list of feed handlers.unregister_feed_handler(my_func)Admin Panel Usage GuideQuickstartStart the server:
You can start the server with uvicorn like this:importosif__name__=="__main__":importuvicornuvicorn.run("agentcomms:start_server",host="0.0.0.0",port=int(os.getenv("PORT",8000)))This will start the server athttp://localhost:8000.Get a file:
Once the server is up and running, you can retrieve file content by sending a GET request to/file/{path}endpoint, where{path}is the path to the file relative to the server's current storage directory.fromagentcommsimportget_file# Fetches the content of the file located at "./files/test.txt"file_content=get_file("test.txt")print(file_content)Save a file:
Similarly, you can save content to a file by sending a POST request to/file/endpoint, with JSON data containing thepathandcontentparameters.fromagentcommsimportadd_file# Creates a file named "test.txt" in the current storage directory# and writes "Hello, world!" to it.add_file("test.txt","Hello, world!")API DocumentationAgentFS provides the following public functions:start_server(storage_path=None)Starts the FastAPI server. If astorage_pathis provided, it sets the storage directory to the given path.Arguments:storage_path(str, optional): The path to the storage directory.Returns:NoneExample:fromagentcommsimportstart_serverstart_server("/my/storage/directory")get_server()Returns the FastAPI application instance.Arguments:NoneReturns:FastAPI application instance.Example:fromagentcommsimportget_serverapp=get_server()set_storage_path(new_path)Sets the storage directory to the provided path.Arguments:new_path(str): The path to the new storage directory.Returns:Trueif the path was successfully set,Falseotherwise.Example:fromagentcommsimportset_storage_pathset_storage_path("/my/storage/directory")add_file(path, content)Creates a file at the specified path and writes the provided content to it.Arguments:path(str): The path to the new file.content(str): The content to be written to the file.Returns:Trueif the file was successfully created.Example:fromagentcommsimportadd_fileadd_file("test.txt","Hello, world!")remove_file(path)Removes the file at the specified path.Arguments:path(str): The path to the file to be removed.Returns:Trueif the file was successfully removed.Example:fromagentcommsimportremove_fileremove_file("test.txt")update_file(path, content)Appends the provided content to the file at the specified path.Arguments:path(str): The path to the file to be updated.content(str): The content to be appended to the file.Returns:Trueif the file was successfully updated.Example:fromagentcommsimportupdate_fileupdate_file("test.txt","New content")list_files(path='.')Lists all files in the specified directory.Arguments:path(str, optional): The path to the directory. Defaults to'.'(current directory).Returns:A list of file names in the specified directory.Example:fromagentcommsimportlist_filesfiles=list_files()list_files_formatted(path='.')Lists all files in the specified directory as a formatted string. Convenient!Arguments:path(str, optional): The path to the directory. Defaults to'.'(current directory).Returns:A string containing a list of file names in the specified directory.Example:fromagentcommsimportlist_filesfiles=list_files()get_file(path)Returns the content of the file at the specified path.Arguments:path(str): The path to the file.Returns:A string containing the content of the file.Example:fromagentcommsimportget_filecontent=get_file("test.txt")Contributions WelcomeIf you like this library and want to contribute in any way, please feel free to submit a PR and I will review it. Please note that the goal here is simplicity and accesibility, using common language and few dependencies.