Teamwire SDK - Basics

With the aim of learning how to develop your own bots with the Teamwire SDK, let's start with some basic examples about how we can start a connection with the backend and retrieve some interesting information about the bot.

We assume that you already created the credentials for your test bot, and that you know the base URL of the Teamwire backend you must connect to. For Cloud deployments the backend is simply https://backend.teamwire.eu/.

Setting up a Connection with the backend

Bot credentials can be set in two ways:

  • by ensuring the TW_API_APPID, TW_API_SECRET and TW_API_HOST environment variables are set before starting the bot
  • by passing the same values in the Connection constructor in the bot code

For example, we can set up a connection with the Teamwire backend in this way:

from teamwire_api import Connection

appid = "APP ID"
secret = "SECRET"
host = "BACKEND URL"

with Connection(appid, secret, host) as connection:
    print(connection.own_user.full_name())

The above code snippet simply prints the bot user's full name and exits.

From now on, we'll omit passing the bot credentials to the Connection constructor, and we'll assume those values are set as environment variables, which will be automatically retrieved by the SDK.

Exploring the User object

In Teamwire, bots and integrations are treated as a special kind of users, with whom they share the same properties like the name and a user id.

While the above example, we only printed out the bot user's full name, we can further explore the other members exposed by the User  class by checking the API reference.

For example:

from teamwire_api import Connection

with Connection() as connection:
    # get the bot user
    bot = connection.own_user

    # print the bot user name and whether it is a bot
    print(f"hi, I am {bot.first_name} - I {'am' if bot.is_bot else 'am not'} a bot")

Please note that the examples above simply open a connection with the backend, directly get some information from it, then they exit.

We are now ready to start receiving messages