Skip to main content

Python package for a Webex Bot based on websockets.

Project description

Introduction

Pypi Build Status

By using this module, you can create a Webex Teams messaging bot quickly in just a couple of lines of code.

This module does not require you to set up an ngrok tunnel to receive incoming messages when behind a firewall or inside a LAN. This package instead uses a websocket to receive messages from the Webex cloud.

Features

  • Uses the websockets module to receive incoming messages, thus avoiding the need to have a public IP or use incoming webhooks.
  • Simply add 'commands' which are just strings which instruct the bot to perform some action and reply with some result.
  • Allows for single or multi-post responses. This is useful if you want to reply with a lot of data, but it won't all fit in a single response.
  • Restrict bot to certain users or domains.
  • Uses the webexteamssdk package to send back replies from the bot.

Getting started

  1. Install this module from pypi:

pip install webex_bot

  1. On the Webex Developer portal, create a new bot token and expose it as an environment variable.
export WEBEX_TEAMS_ACCESS_TOKEN=<your bots token>
  1. Run your script:

python example.py

See example.py for details:

import os

from webex_bot.commands.echo import EchoCommand
from webex_bot.webex_bot import WebexBot

# Create a Bot Object
bot = WebexBot(teams_bot_token=os.getenv("WEBEX_TEAMS_ACCESS_TOKEN"))

# Add new commands for the bot to listen out for.
bot.add_command(EchoCommand())

# Call `run` for the bot to wait for incoming messages.
bot.run()

where EchoCommand is defined as:

import logging

from webex_bot.cards.echo_card import ECHO_CARD_CONTENT
from webex_bot.formatting import quote_info
from webex_bot.models.command import Command

log = logging.getLogger(__name__)


class EchoCommand(Command):

    def __init__(self):
        super().__init__(
            command_keyword="echo",
            help_message="Type in something here and it will be echo'd back to you. How useful is that!",
            card=ECHO_CARD_CONTENT)

    def execute(self, message, attachment_actions):
        """
        If you want to respond to a submit operation on the card, you
        would write code here!

        You can return text string here or even another card (Response).

        This sample command function simply echos back the sent message.

        :param message: message with command already stripped
        :param attachment_actions: attachment_actions object
        :return: a string or Response object. Use Response if you want to return another card.
        """
        return quote_info(attachment_actions.inputs.get("message_typed"))
  1. Now, just interact 1-1 with the bot. Send it a message with the text:

echo

and off you go!

History

0.1.2 (2021-03-15)

  • First release on PyPI.

0.1.4 (2021-03-23)

  • Better retry on websocket connection failure
  • Added support for approved domains
  • Other cleanup

0.1.5 (2021-03-23)

  • Retry websocket connection on socket.gaierror. (#1)

0.1.6 (2021-03-25)

  • Send ack on websocket message received. (#2)

0.1.7 (2021-03-26)

  • Minor cleanup.
  • Log bot email on startup.

0.1.8 (2021-05-04)

  • Fetch incoming message ID as base64.

0.2.0 (2021-05-07)

  • Switch format entirely to use cards.

0.2.1 (2021-05-07)

  • Import fix

0.2.2 (2021-05-08)

  • Typo fix

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

webex_bot-0.2.2.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

webex_bot-0.2.2-py2.py3-none-any.whl (14.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file webex_bot-0.2.2.tar.gz.

File metadata

  • Download URL: webex_bot-0.2.2.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

File hashes

Hashes for webex_bot-0.2.2.tar.gz
Algorithm Hash digest
SHA256 1802341edd955e341f415d430dbfe6d7c3a64a53ce1f643ca9c8ca45c15d28dc
MD5 55dabdcf5f470f1c375abb4c5925525a
BLAKE2b-256 1c54c772c530fca84c1d2c16c2f04dcf7143f944e27d0cf6e95309dbbe769a0f

See more details on using hashes here.

File details

Details for the file webex_bot-0.2.2-py2.py3-none-any.whl.

File metadata

  • Download URL: webex_bot-0.2.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

File hashes

Hashes for webex_bot-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e026a3576d48705077481805bcfef90943f9d924343f53aff10168d7ed860187
MD5 098c86c5a8be131d4879b913bc9c943f
BLAKE2b-256 8a5e3e9d4d8ce2bf0ae07d496e2b781b5430efa179c46b427e21f112b9aa5288

See more details on using hashes here.

Supported by

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