Git Product home page Git Product logo

tock-py's Introduction

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()

tock-py's People

Contributors

aluriak avatar dependabot[bot] avatar elebescond avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.