Skip to content

Clients

Simon Olofsson edited this page Sep 8, 2021 · 18 revisions

Client support and API

Pyttman enables you as a developer to write a digital assistant / chatbot without re-inventing the wheel every time you want to extend its functionality or develop another bot for a different use case, and also to use your app with different platforms without re-writing your app for each one. A part of this is the Client support in Pyttman.

What is a Client anyway?

When you communicate with your chatbot, you're doing it through some kind of platform. It could be anything from a website chat component, a platform such as Discord or Telegram - it may even be a plain console window.

On your chatbot's side, these platforms integrate with your Pyttman app through clients. One client for each plattform is the idea.

Since plattforms use different schemas of their API's and not all interfaces look the same, Pyttman comes with an interface of the BaseClient client class.

The BaseClient class ensures that all clients used with Pyttman conform to a minimal standard of properties and methods.

Ready-to-use clients

Pyttman 1.1.4 comes pre-packaged with two clients:

  • CLIClient ( CommandLineInterfaceClient ) - chat with your Pyttman app in a terminal window
  • DiscordClient - Host your Pyttman app as a Discord chatbot

BaseClient(*args, message_router: AbstractMessageRouter, **kwargs)

Baseclass for Clients, for interfacing with users directly or through an API.

Attributes

  • message_router - MessageRouter

Methods

  • run_client(self)
    Starts the main method for the client, opening
    a session to the front end with which it is
    associated with.
    :return: None

CliClient(*args, message_router: AbstractMessageRouter, **kwargs)

The CliClient class is the most basic client class. It will run in the terminal window where the app is started, and show a simple command line interface in which you can write commands to your Pyttman app.

Attributes

  • message_router - MessageRouter

Methods

  • run_client(self)

    Starts the main method for the client, opening
    a session to the front end with which it is
    associated with.
    :return: None
    
  • publish(reply: Reply)

    Publish a message to the user.
    
    :param reply: Reply object
    :return: None
    

How to use with settings.py in a Pyttman app

# settings.py

CLIENTS = [
	{
		"class": "pyttman.clients.builtin.cli.CliClient"
	}
]

DiscordClient(*args, message_router: AbstractMessageRouter, **kwargs)

Note!

This class inherits from the discord.py module's Client class. All methods and attributes are therefore available. on_message has been overloaded in this class to integrate the Discord on_message response with what your app produces as a Reply.

For other available hooks in this class, please read the discord Client documentation available here.

Attributes

  • message_router - MessageRouter

  • token - str, token string from discord developer portal for your bot app

  • guild - str - guild ID for the server on which the bot should join on

  • message_startswith - define what messages have to start with for your app to register them. Can be a single character or a word, equal to str().startswith().

  • message_endswith - define what messages have to end with for your app to register them. Can be a single character or a word, equal to str().endswith().

Methods

  • on_message(self)

    Overloads on_message in discord.Client().The
    method is called implicitly whenever an incoming
    message over the discord interface is intercepted.
    All messages are parsed, thus it's a good idea to
    verify that the author is not self to prevent endless
    loops.
    :param message: discord.Message
    :return: None
    
  • run_client(self)

    Starts the main method for the client, opening
    a session to the front end with which it is
    associated with.
    :return: None
    

Tip!

if you want to define hooks for your Discord bot, such as the on_member_join or any other available event hook which is not defined in the DiscordClient by default, you can subclass DiscordClient and overload these hooks as you please.

How to use with settings.py in a Pyttman app

Note!

Remember to use the import reference in settings.py in the class field instead of the DiscordClient import reference, e.g. myapp.client.MyCustomDiscordClient

# settings.py

CLIENTS = [
	{
		"class": "pyttman.clients.community.discord.client.DiscordClient",
		"token": "TOKEN-FROM-DISCORD-DEV-PORTAL",
		"guild": "GUILD-ID-FOR-YOUR-DISCORD-SERVER",
                "message_startswith": "!",                # This is optional.
                "message_endswith": "?"                   # This is optional.
	}
]

Welcome to the Pyttman Wiki! Here you'll find all available documentation and tutorials for Pyttman.


Get started


Documentation


Contributors

Clone this wiki locally