Wallet messaging with Agents
This guide explains how to use the uagents
library to enable wallet-to-wallet messaging between two Agents: Alice
and Bob
.
Walk-through
We begin by importing needed packages and the by defining the Agents Alice
and Bob
, by defining a name
, seed
and enable_wallet_messaging
parameters. Here, enable_wallet_messaging=True
ensures the Agents can send and receive wallet messages.
from uagents import Agent, Bureau, Context
from uagents.wallet_messaging import WalletMessage
# Define recovery phrases (seeds) for Alice and Bob
ALICE_SEED = "alice dorado recovery phrase"
BOB_SEED = "bob dorado recovery phrase"
# Initialize agents with wallet messaging enabled
alice = Agent(name="alice", seed=ALICE_SEED, enable_wallet_messaging=True)
bob = Agent(name="bob", seed=BOB_SEED, enable_wallet_messaging=True)
We then go on and define the Agents’ handlers. Alice listens for incoming wallet messages using the .on_wallet_message()
handler. When she receives a message, it logs it and sends a reply back to the sender:
@alice.on_wallet_message()
async def reply(ctx: Context, msg: WalletMessage):
ctx.logger.info(f"Got wallet message: {msg.text}")
await ctx.send_wallet_message(msg.sender, "hey, thanks for the message")
Bob sends a message to Alice every 5 seconds using the .on_interval()
handler. Also, Bob listens for incoming wallet messages, just like Alice. This ensures Bob can receive and log messages from other Agents:
@bob.on_interval(period=5)
async def send_message(ctx: Context):
ctx.logger.info("Sending message...")
await ctx.send_wallet_message(alice.address, "hello")
@bob.on_wallet_message()
async def wallet_reply(ctx: Context, msg: WalletMessage):
ctx.logger.info(f"Got wallet message: {msg.text}")
Finally, both Alice and Bob are added to a Bureau, which runs the Agents concurrently:
bureau = Bureau()
bureau.add(alice)
bureau.add(bob)
bureau.run()
from uagents import Agent, Bureau, Context
from uagents.wallet_messaging import WalletMessage
ALICE_SEED = "alice dorado recovery phrase"
BOB_SEED = "bob dorado recovery phrase"
alice = Agent(name="alice", seed=ALICE_SEED, enable_wallet_messaging=True)
bob = Agent(name="bob", seed=BOB_SEED, enable_wallet_messaging=True)
@alice.on_wallet_message()
async def reply(ctx: Context, msg: WalletMessage):
ctx.logger.info(f"Got wallet message: {msg.text}")
await ctx.send_wallet_message(msg.sender, "hey, thanks for the message")
@bob.on_interval(period=5)
async def send_message(ctx: Context):
ctx.logger.info("Sending message...")
await ctx.send_wallet_message(alice.address, "hello")
@bob.on_wallet_message()
async def wallet_reply(ctx: Context, msg: WalletMessage):
ctx.logger.info(f"Got wallet message: {msg.text}")
bureau = Bureau()
bureau.add(alice)
bureau.add(bob)
bureau.run()