Skip to main content

Python 3 API that allows interactive access to the TeamSpeak 3 Server Query interface.

Project description

Simple Python API for the Teamspeak 3 Server Query API

Python Package Building CodeQL Pylint

Python 3 API that allows interactive access to the Teamspeak 3 Server Query interface.

Installation

Either clone the GitHub repository and use local imports, or install it via pip:

pip install ts3API

If you want to use SSH connections also install the optional dependency Paramiko:

pip install paramiko

Code Example

from ts3API.TS3Connection import TS3Connection
import ts3API.Events as Events

HOST = "serverhost"
PORT = 10011 # Default Port
USER = 'serveradmin' # Default login
PASS = 'password'
DEFAULTCHANNEL = 'Botchannel-or-any-other'
SID = 1 # Virtual server id
NICKNAME = "aName"

def on_event(sender, **kw):
    """
    Event handling method
    """
    # Get the parsed event from the dictionary
    event = kw["event"]
    print(type(event))
    """
    # This generates output for every event. Remove the comment if you want more output
    for attr, value in event.__dict__.items():
        print("\t"+attr+":", value)
    """
    if isinstance(event, Events.ClientBannedEvent):
        print("Client was banned!")
        print("\tClient ID:", event.client_id)
        print("\tReason Message:", event.reason_msg)
        print("\tInvokerID:", event.invoker_id)
        print("\tInvokerName:", event.invoker_name)
        print("\tBantime:", event.ban_time)
    if isinstance(event, Events.ClientKickedEvent):
        print("Client was kicked!")
        print("\tClient ID:", event.client_id)
        print("\tReason Message:", event.reason_msg)
        print("\tInvokerID:", event.invoker_id)
        print("\tInvokerName:", event.invoker_name)

    if isinstance(event, Events.ClientLeftEvent):
        print("Client left!")
        print("\tClient ID:", event.client_id)
        print("\tReason Message:", event.reason_msg)
    if type(event) is Events.TextMessageEvent:
        # Prevent the client from sending messages to itself
        if event.invoker_id != int(ts3conn.whoami()["client_id"]):
            ts3conn.sendtextmessage(targetmode=1, target=event.invoker_id, msg="I received your message!")

# Connect to the Query Port
ts3conn = TS3Connection(HOST, PORT)
# Login with query credentials
ts3conn.login(USER, PASS)
# Choose a virtual server
ts3conn.use(sid=SID)
# Find the channel to move the query client to
channel = ts3conn.channelfind(pattern=DEFAULTCHANNEL)[0]["cid"]
# Give the Query Client a name
ts3conn.clientupdate(["client_nickname="+NICKNAME])
# Move the Query client
ts3conn.clientmove(channel, int(ts3conn.whoami()["client_id"]))
# Register for server wide events
ts3conn.register_for_server_events(on_event) 
# Register for private messages
ts3conn.register_for_private_messages(on_event)
# Register for channel message in botchannel
ts3conn.register_for_channel_events(channel, on_event) 
# Start the loop to send connection keepalive messages
ts3conn.start_keepalive_loop()

For a more elaborated example of this API see the ts3Bot project: https://github.com/Murgeye/ts3Bot

Calling functions not explicitly implemented

Thanks to Chrisg2000's contribution, the API implements any command accepted by a TeamSpeak3 server. You can call any command mentionend in the server query manual (should come with you server installation) using keyword arguments, even if it's not explicitly implemented in code. See this code snippet for example:

servergroupaddclient(sgid=servergroup_id, cldbid=client_db_id)  

The servergroupaddclient command is not currently implemented explicitly. However, you can still call it if you know the parameters it need (sgid and cldbid).

Troubleshooting

For general troubleshooting please also have a look at the troubleshooting section in https://github.com/Murgeye/ts3Bot. If any questions remain, feel free to open an issue.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ts3API-0.9.4.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

ts3API-0.9.4-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file ts3API-0.9.4.tar.gz.

File metadata

  • Download URL: ts3API-0.9.4.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for ts3API-0.9.4.tar.gz
Algorithm Hash digest
SHA256 e8b4f3ca1b8c9a2be60f328c222e833300e58ccfe5fb2205e949c13ff4ee9cfb
MD5 494e40dd6724b7be9dc30bed61a6230a
BLAKE2b-256 c4e62151088564c24acec1a64135906feeb73515f97dea6b7918c6c64b8d41b5

See more details on using hashes here.

File details

Details for the file ts3API-0.9.4-py3-none-any.whl.

File metadata

  • Download URL: ts3API-0.9.4-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for ts3API-0.9.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5d7704f36815bf6d4333bcefb952d6ccde068254e02daa09dbc370c7cdccd67f
MD5 00f85971c7d5805f3c129c094acbc656
BLAKE2b-256 d8b332bc394439ea3d46f74f7bcc284b4c6f497b8eb5acd97005c48f39113a95

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page