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 of how we can connect to 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_SECRETandTW_API_HOSTenvironment variables are set before starting the bot - by passing the same values in the
Connectionconstructor 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 user, with whom they share the same properties, such as a name and user ID.
In the above example, we only printed the bot user's full name, but we
can further explore the other members exposed by the
class by checking the API reference.User
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, retrieve some information from it and then exit.
We are now ready to start receiving messages.