Utilizing the Agentverse Mailroom service 📬
Introduction
The Agentverse Mailroom assists you in setting up mailboxes for local and Agentverse agents, allowing them to have a two-way communication with each other without the need to be constantly online and without requiring your constant presence to operate.
Local agent setup
Let's now start by creating a local agent named alice
with a handle_message()
function using an @agent.on_message()
decorator to handle messages received by other agents and matching the Message
class:
from uagents import Agent, Context, Model
class Message(Model):
message: str
# First generate a secure seed phrase (e.g. https://pypi.org/project/mnemonic/)
SEED_PHRASE = "put_your_seed_phrase_here"
# Copy the address shown below
print(f"Your agent's address is: {Agent(seed=SEED_PHRASE).address}")
# Then go to https://agentverse.ai, register your agent in the Mailroom
# and copy the agent's mailbox key
AGENT_MAILBOX_KEY = "put_your_AGENT_MAILBOX_KEY_here"
# Now your agent is ready to join the agentverse!
agent = Agent(
name="alice",
seed=SEED_PHRASE,
mailbox=f"{AGENT_MAILBOX_KEY}@https://agentverse.ai",
)
@agent.on_message(model=Message, replies={Message})
async def handle_message(ctx: Context, sender: str, msg: Message):
ctx.logger.info(f"Received message from {sender}: {msg.message}")
# send the response
ctx.logger.info("Sending message to bob")
await ctx.send(sender, Message(message="hello there bob"))
if __name__ == "__main__":
agent.run()
Agentverse agent setup
Now create an Agentverse agent bob
by selecting + New Agent in the My Agents tab in the Agentverse ↗️ (opens in a new tab). Then, add the following code to it:
from uagents import Agent, Context, Model
class Message(Model):
message: str
# Copy ALICE_ADDRESS generated in alice.py
ALICE_ADDRESS = "paste_alice_address_here"
# Generate a second seed phrase (e.g. https://pypi.org/project/mnemonic/)
SEED_PHRASE = "put_your_seed_phrase_here"
# Copy the address shown below
print(f"Your agent's address is: {Agent(seed=SEED_PHRASE).address}")
# Then go to https://agentverse.ai, register your agent in the Mailroom
# and copy the agent's mailbox key
AGENT_MAILBOX_KEY = "put_your_AGENT_MAILBOX_KEY_here"
# Now your agent is ready to join the agentverse!
agent = Agent(
name="bob",
seed=SEED_PHRASE,
mailbox=f"{AGENT_MAILBOX_KEY}@https://agentverse.ai",
)
@agent.on_interval(period=2.0)
async def send_message(ctx: Context):
ctx.logger.info("Sending message to alice")
await ctx.send(ALICE_ADDRESS, Message(message="hello there alice"))
@agent.on_message(model=Message, replies=set())
async def on_message(ctx: Context, sender: str, msg: Message):
ctx.logger.info(f"Received message from {sender}: {msg.message}")
if __name__ == "__main__":
agent.run()
Next, run bob on the Agentverse. Finally, run your local agent and you will see alice
's local agent messages printed on bob
's Agentverse terminal (i.e. the Agents Logs).