# Ping-Pong

## What are we building?

Let's start with a dead simple example to understand how AgentLabs works.

In this recipe, we'll create a basic server with one single agent that listens for an incoming message.

Every time a user sends a message through the Frontend UI, the agent will evaluate it and answer.

If the message is equal to `ping` the agent will reply with `pong`.&#x20;

Otherwise, the agent will say he did not get the message.

The full example is available in [our example repository](https://github.com/agentlabs-inc/agent-examples/tree/main/nodejs-examples/ts-ping-pong).

## Requirements

To follow this tutorial, you must have:

* some basic programming skills in Python or Typescript
* created a project and an agent in [your console](https://console.agentlabs.dev)

## Let's code

### Initializing the SDK

First, we must initialize the SDK with the project ID and the secret key.

You can find this information on your [admin console](https://console.agentlabs.dev/admin/).

{% tabs %}
{% tab title="Typescript" %}
{% code fullWidth="false" %}

```typescript
import { Project } from 'agentlabs-sdk';

const project = new Project({
    projectId: 'you-project-id',
    secret: 'your-secret',
});

const agent = project.agent('agent-id');
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}

```python
from agentlabs.chat import IncomingChatMessage, MessageFormat
from agentlabs.project import Agent, Project
project = Project(
        project_id='your-project-id',
        secret='your-secret'
)

agent = project.agent('agent-id')
```

{% endtab %}
{% endtabs %}

### Listen for messages

Now, we can use the `onChatMessage` method to apply some logic every time the user sends a message in the chat.

The `onChatMessage` accepts a callback as an argument, this callback will handle our logic.

Then, we'll use the `send` method exposed on the agent instance to send a message back to the chat on behalf of the agent.

{% hint style="info" %}
Note the message argument contains some useful data, such as the conversation ID or the message's text.
{% endhint %}

{% tabs %}
{% tab title="Typescript" %}

```typescript
import { Project } from 'agentlabs-sdk';

const project = new Project({
    projectId: 'you-project-id',
    secret: 'your-secret',
});

const agent = project.agent('agent-id');

project.onChatMessage((message) => {
  if (message.text === 'ping') {
     agent.send({
        conversationId: message.conversationId,
        text: 'pong'
     })
  } else {
     agent.send({
        conversationId: message.conversationId,
        text: 'I did not get that'
     })
  }
})
```

{% endtab %}

{% tab title="Python" %}

```python
from agentlabs.chat import IncomingChatMessage, MessageFormat
from agentlabs.project import Agent, Project

project = Project(
        project_id='your-project-id',
        secret='your-secret'
)

agent = project.agent('agent-id')

def handle_message(message: IncomingChatMessage):
    if message.text == 'ping':
        agent.send(
            conversation_id=message.conversation_id,
            text="pong"
        )
    else:
        agent.send(
            conversation_id=message.conversation_id,
            text="I did not understand"
        )

project.on_chat_message(handle_message)
```

{% endtab %}
{% endtabs %}

### Init the connection

So far we instantiated our project and agent instances. We also defined the behavior of the agent.

Now, we need to initiate the connection between your server and ours.&#x20;

No worries, it's the simplest part. We just have to use the `project.connect()` method.

{% hint style="info" %}
Note in Python you will have to use the `project.wait()` in addition to `project.connect()`.
{% endhint %}

{% tabs %}
{% tab title="Typescript" %}

```typescript
import { Project } from 'agentlabs-sdk';

const project = new Project({
    projectId: 'you-project-id',
    secret: 'your-secret',
});

const agent = project.agent('agent-id');

project.onChatMessage((message) => {
  if (message.text === 'ping') {
     agent.send({
        conversationId: message.conversationId,
        text: 'pong'
     })
  } else {
     agent.send({
        conversationId: message.conversationId,
        text: 'I did not get that'
     })
  }
})

project.connect()
```

{% endtab %}

{% tab title="Python" %}

```python
from agentlabs.chat import IncomingChatMessage, MessageFormat
from agentlabs.project import Agent, Project

project = Project(
        project_id='your-project-id',
        secret='your-secret'
)

agent = project.agent('agent-id')

def handle_message(message: IncomingChatMessage):
    if message.text == 'ping':
        agent.send(
            conversation_id=message.conversation_id,
            text="pong"
        )
    else:
        agent.send(
            conversation_id=message.conversation_id,
            text="I did not understand"
        )

project.on_chat_message(handle_message)

project.connect()
project.wait();
```

{% endtab %}
{% endtabs %}

### :tada:Congrats

You did it! Here's the result you must have :relaxed:

{% embed url="<https://youtu.be/020oBT4Maec>" %}
