Skip to content

Latest commit

 

History

History
170 lines (136 loc) · 3.96 KB

File metadata and controls

170 lines (136 loc) · 3.96 KB

Use

Use with ringcentral_chatbot_server cli

rcs your_bot_config.py

Use in Local development with Flask server

from flask import Flask, request
from dotenv import load_dotenv
load_dotenv()
import os, sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../..')
from ringcentral_bot_framework import frameworkInit

# put all bot logic in `config.py`, check `sample-bots/kitchen-sync.py` to see all the config functions
import config as conf

framework = frameworkInit(conf)

app = Flask('devtest')

@app.route('/<action>', methods=['GET', 'POST'])
def act(action):
  event = framework.flaskRequestParser(request, action)
  response = framework.router(event)
  resp = response['body']
  headers = {}
  if 'headers' in response:
      headers = response['headers']
  return resp, response['statusCode'], headers

port = 9898
host = 'localhost'

app.run(
  host=host,
  port=port,
  debug=True,
  load_dotenv=True
)

Use in AWS Lambda

from ringcentral_bot_framework import frameworkInit
import config as conf

framework = frameworkInit(conf)

def bot(event, context):

    return framework.router(event)

Framework member functions and properties

Read source code for more detail.

  class BotFrameWork:
    @staticmethod
    def Bot():
      return BotClass

    @staticmethod
    def User():
      return UserClass

    @staticmethod
    def dbAction(tableName, action, data = None):
      """db action
      * make sure it it stateless,
      * in every action, you should check database is ready or not, if not, init it first
      * check https://github.com/ringcentral/ringcentral-chatbot-python/blob/master/ringcentral_bot_framework/core/dynamodb.py or https://github.com/ringcentral/ringcentral-chatbot-python/blob/master/ringcentral_bot_framework/core/filedb.py as example
      * @param {String} tableName, user or bot, or other table you defined
      * @param {String} action, add, remove, update, get
      * @param {Object} data
      * for add, {'id': 'xxx', 'token': {...}, 'groups': {...}, 'data': {...}}
      * for remove, {'id': xxx} or {'ids': [...]}
      * for update, {'id': xxx, 'update': {...}}
      * for get, singleUser:{'id': xxx}, allUser: None, query: { 'key': 'xx', 'value': 'yy' }
      """
      return dbAction(tableName, action, data = None)

    @staticmethod
    def getBot(id):
      '''
      get bot data from database, init and return bot instance, if fails or not found will return False
      '''
      return getBot(id)

    @staticmethod
    def removeBot(id):
      '''
      remove bot data from database
      '''
      return removeBot(id)

    @staticmethod
    def getUser(id):
      '''
      get user data from database, init and return user instance, if fails or not found will return False
      '''
      return getUser(id)

    @staticmethod
    def removeUser(id):
      '''
      remove user data from database
      '''
      return removeUser(id)

    @staticmethod
    def botAuth(event):
      return botAuth(event)

    @staticmethod
    def renewBot(event):
      return renewBot(event)

    @staticmethod
    def botWebhook(event):
      return botWebhook(event)

    @staticmethod
    def dataView(event):
      return dataView(event)

    @staticmethod
    def userAuth(event):
      return userAuth(event)

    @staticmethod
    def userWebhook(event):
      return userWebhook(event)

    @staticmethod
    def router(event):
      '''
      process event and return result object:
      {
        'headers': dict,
        'body': dict,
        'statusCode': number
      }
      '''
      return router(event)

    @staticmethod
    def flaskRequestParser(request, action):
      '''
      parse flask request to event format:
      {
        'pathParameters': {
          'action': string
        },
        'queryStringParameters': dict,
        'body': dict,
        'headers': dict
      }
      '''
      return flaskRequestParser(request, action)