Skip to main content

Build chatbots using Tock and Python

Project description

tock-py

Build chatbots using Tock and Python

DISCLAIMERS

  • Work in progress
  • Not production ready
  • not yet implemented
    • Managing User / conversational context
    • Testing

Prerequisites

Run a Tock bot in API mode

Create a Bot application using the web connector type in Tock Studio and get your API key

Environment

We suggest you to create an isolated Python virtual environment:

$ python3 -m venv env
$ source env/bin/activate

Install tock-py on your project

$ pip install tock-py

Usage

import logging
import os

from tock.bot import TockBot
from tock.bus import TockBotBus
from tock.story import story

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.DEBUG
)

# First create story that handle "greetings" intent
@story(
    intent="greetings",
    other_starter_intents=[],
    secondary_intents=[]
)
def greetings(bus: TockBotBus):
    bus.send("Hello i'm a tock-py bot")

# If decorator @story is not provided, the intention with the function name is user
def goodbye(bus: TockBotBus):
    bus.send("Hello i'm a tock-py bot")

# Configure your bot and start it
TockBot() \
    .register_story(greetings) \
    .register_story(goodbye) \
    .start_websocket(apikey=os.environ['TOCK_APIKEY'])


# You can also use webhook mode
# TockBot() \
#     .register_story(greetings) \
#     .register_story(goodbye) \
#     .start_webhook("0.0.0.0", "tock-py", 5000)

Sentence

bus.send("Hello i'm a tock sentence")

Suggestion

bus.send(
    Sentence.Builder("Hello i'm a tock sentence")
        .add_suggestion("with suggestion")
        .build()
)

Card with action

bus.send(
    Card
        .Builder()
        .with_title("card title")
        .with_sub_title("with subtitle")
        .with_attachment("https://www.sncf.com/themes/sncfcom/img/favicon.png", AttachmentType.IMAGE)
        .add_action("visit", "http://www.sncf.com")
        .build()
)

Carousel

card = Card \
    .Builder() \
    .with_title("Card title") \
    .with_sub_title("wit subtitle") \
    .with_attachment("https://www.sncf.com/themes/sncfcom/img/favicon.png", AttachmentType.IMAGE) \
    .add_action("visit", "http://www.sncf.com") \
    .build()
    
bus.send(
    Carousel
        .Builder()
        .add_card(card)
        .add_card(card)
        .add_card(card)
        .build()
)

Custom state

Your bot can store custom state data in session

def greetings(bus):
    if bus.session.get_item("greetings_flag") is None:
        bus.send("Welcome")
    else:
        bus.send("Welcome back")
    bus.session.set_item("greetings_flag", True)

You can clear session

def goodbye(bus):
    bus.session.clear()

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

tock_py-0.1.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tock_py-0.1.0-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file tock_py-0.1.0.tar.gz.

File metadata

  • Download URL: tock_py-0.1.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for tock_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 31e8c14cb24b0b4a02c2bf5bd11e8ea332d259b3557bc74422ccdcda5b258158
MD5 9bc8616a575529f96d37e4763ebd4daa
BLAKE2b-256 ba7138ff3a93d95ebafca02aa382747d9badc8e620a97b7744b2b991310e1101

See more details on using hashes here.

File details

Details for the file tock_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tock_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for tock_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e7a082593ff8e914faecb79b63abaf3c9ffca0f279da514963dc7f63a009de93
MD5 c62a084179816764463388f177d2366c
BLAKE2b-256 3618c5514d014257c0c8589e737c62eb5c70f32b5c51722e81ea0eb07be80a57

See more details on using hashes here.

Supported by

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