Skip to content

Instantly share code, notes, and snippets.

@sld
Last active August 3, 2018 19:02
Show Gist options
  • Save sld/8fc15151d40183d8925e8c67e620a557 to your computer and use it in GitHub Desktop.
Save sld/8fc15151d40183d8925e8c67e620a557 to your computer and use it in GitHub Desktop.
Api Documentation

Quick start

Get the script api_tutorial.py below and run it:

python3 api_tutorial.py http://35.159.33.67:5000

Start chat

Starts chat and returns chat_id.

  • URL

    /start

  • Method:

    POST

  • Data Params

    Required:

    text=[string]

  • Success Response:

    • Code: 200
      Content: { chat_id : '0eb17eb4-c2cf-4b6d-943b-4bad780c2e91' }
  • Sample Call:

    import requests
    base_url = 'http://35.159.33.67:5000'
    url = base_url + '/start'
    r = requests.post(url, json={'text': text})
    print(r.json())

Send message to the bot


Sends message to the bot and get response.

  • URL

    /message

  • Method:

    POST

  • Data Params

    Required:

    text=[string] chat_id=[string]

  • Success Response:

    • Code: 200
      Content: { text : 'Hello!' }
  • Sample Call:

    import requests
    base_url = 'http://35.159.33.67:5000'
    url = base_url + '/message'
    r = requests.post(url, json={'text': 'Hi', 'chat_id': chat_id})
    print(r.json())

Finish the chat


Finish the chat with bot.

  • URL

    /end

  • Method:

    POST

  • Data Params

    Required:

    chat_id=[string]

  • Success Response:

    • Code: 200
      Content: { 'message': 'ok' }
  • Sample Call:

    import requests
    base_url = 'http://35.159.33.67:5000'
    url = base_url + '/end'
    r = requests.post(url, json={'chat_id': chat_id})
    print(r.json())
import requests
import json
from sys import argv
def start_chat(url, text=''):
url = url + '/start'
r = requests.post(url, json={'text': text})
return r.json()
def message(url, chat_id, text):
url = url + '/message'
r = requests.post(url, json={'text': text, 'chat_id': chat_id})
return r.json()
def message_with_print(url, chat_id, text):
print('HUMAN: {}'.format(text))
res = message(url, chat_id, text)
print('BOT: {}'.format(res['text']))
def end_chat(url, chat_id):
url = url + '/end'
r = requests.post(url, json={'chat_id': chat_id})
return r.json()
def conversation_example(url):
text = ("The name-letter effect is the tendency of people to prefer the letters"
" in their name over other letters in the alphabet. Discovered in 1985"
" by the Belgian psychologist Jozef Nuttin, the effect has been replicated "
" in dozens of studies.")
chat_id = start_chat(url, text)['chat_id']
message_with_print(url, chat_id, 'Hello dear friend!')
message_with_print(url, chat_id, 'How are you?')
message_with_print(url, chat_id, 'What is your name?')
message_with_print(url, chat_id, 'What was discovered in 1985?')
message_with_print(url, chat_id, 'I have to go. Bye!')
end_chat(url, chat_id)
if __name__ == '__main__':
url = argv[1]
conversation_example(url)
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment