🐍 Python Client#

Prerequisites

If you have not installed a daemon, please follow the section.

Before you start, make sure that you have an instance of the Olvid daemon running, with a identity created on it, and a valid client key to connect using this identity.

If you want to check that your daemon is functional and/or recover a lost client key, use the following command: docker compose run --rm cli key get

Installation#

We have developed a Python library that allows you to easily interact with your daemon. It is available for installation with the command pip.

pip3 install olvid-bot

Tip

The module olvid-bot requires a version of Python higher or equal to 3.10. On Mac OS, we recommend using Homebrew to install a more recent version.

brew install python3

Configuration#

To connect to the daemon, your program needs a client key. This key can be passed to your program using either an environment variable or a .env file.

.env file

Create a .env file using the following command. Remember that this file should be in the current directory when you run your program.

echo OLVID_CLIENT_KEY=ReplaceWithYourClientKey > .env
Environment variable

It is also possible to export your key as an environment variable. In this case, remember to export it at each new shell session.

export OLVID_CLIENT_KEY=ReplaceWithYourClientKey

First program#

In order to verify that everything works correctly, you can copy/paste this program into a file named main.py.

import asyncio
from olvid import OlvidClient

async def main():
    client = OlvidClient()
    print((await client.identity_get()).display_name)

asyncio.set_event_loop(asyncio.new_event_loop())
asyncio.get_event_loop().run_until_complete(main())

You can then run it using the Python interpreter.

python3 main.py

If everything has been properly configured, you should see the display name of the identity you created on the daemon.

Alternatively, visit our πŸ‘©β€πŸ”§ Troubleshooting section.

First Bot#

To start coding your first bot, you can use the following example. It simply responds to each message with the same message, but it can easily be modified and improved.

import asyncio
from olvid import OlvidClient, datatypes, tools

class EchoBot(OlvidClient):
    async def on_message_received(self, message: datatypes.Message):
        await message.reply(message.body)

    async def on_discussion_new(self, discussion: datatypes.Discussion):
        await discussion.post_message("Hello πŸ‘‹")

async def main():
    bot = EchoBot()
    tools.AutoInvitationBot()  # automatically accept presentation and group invitations
    await bot.run_forever()

asyncio.set_event_loop(asyncio.new_event_loop())
asyncio.get_event_loop().run_until_complete(main())

What’s next?#

To learn more about using our Python library, visit our πŸ‘©β€πŸ« Tutorials section. Otherwise, head to the 🌱 Examples section for ready-to-use projects.