MrMenezes
11/6/2019 - 10:24 PM

apptrest

from flask import Flask, request
from flask_restplus import Api, Resource, fields
import requests
from time import time
import json

app = Flask(__name__)
api = Api(app, version='1.0', title='BotMVC API',
    description='A simple BotMVC API',
)

ns = api.namespace('bot', description='Bot operations')

bot = api.model('Bot', {
    'name': fields.String(readOnly=True, description='Name of user'),
    'cpf': fields.String(required=True, description='CPF of user'),
    'email': fields.String(required=True, description='Email of user'),
    'token': fields.String(required=True, description='Token in Hibot URL')
})

text = api.model('Text', {
    'text': fields.String,
})

class BotDao(object):
    def __init__(self, args, **kwargs):
        print(args)
        self.name = args.get('name')
        self.finger_print = str(int(time()))
        self.department_id = args.get('token')
        self.cpf = args.get('cpf')
        self.email = args.get('email')
        self.session_id = kwargs.get('session_id')
        self.headers = headers = {
            'Sec-Fetch-Mode': 'cors',
            'Origin': 'https://cdn-bot.hiplatform.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Referer': 'https://cdn-bot.hiplatform.com/1.71.8/index.html',
            'User-Name': self.name,
            'Customer-Fingerprint': self.finger_print,
            'Customer-Session': self.session_id
        }

    def auth(self):
        data = '{"departmentId":"' + self.department_id + '","keys":[{"key":"cpf","value":"' + self.cpf + '"}],"properties":[{"key":"origem","value":""},{"key":"nome_usuario","value":"' + self.name + '"},{"key":"cpf","value":"' + self.cpf + '"},{"key":"email","value":"' + self.email + '"}],"subjects":[]}'
        response = requests.post('https://dtbot.directtalk.com.br/1.0/api/History/Start', headers=self.headers, data=data)
        return response.text.replace('"','')

    def intro(self):
        params = (
            ('key', 'introduction'),
        )
        response = requests.get('https://dtbot.directtalk.com.br/1.0/api/History/Message/', headers=self.headers, params=params)
        return json.loads(response.text)

    def article(self, limit):
        response = requests.get('https://dtbot.directtalk.com.br/1.0/api/articles/' + self.department_id + '/limit/' + str(limit), headers=self.headers)
        return response.text

    def search(self, text):
        data = '{"department_id":"' + self.department_id + '","text":"' + text + '"}'
        response = requests.post('https://dtbot.directtalk.com.br/1.0/api/Search', headers=self.headers, data=data)
        return json.loads(response.text)

    def key_error(self):
        params = (
            ('key', 'errorline'),
        )
        response = requests.get('https://dtbot.directtalk.com.br/1.0/api/History/Message/', headers=self.headers,  params=params)
        return json.loads(response.text)

@ns.route('/auth')
class BotAuth(Resource):

    @ns.doc('auth_bot')
    @ns.expect(bot)
    def post(self):
        '''Auth a new bot'''
        bot = BotDao(api.payload)
        session_id = bot.auth()
        print(session_id)
        return session_id, 201
    '''
    Example:
        {
        "name": "XXXX Menezes",
        "cpf": "05493074516",
        "email": "sr.tama@outlook.com",
        "token": "1bc01b47-d1b0-45dc-9cfc-dcfee1771752"
        }
    '''
session = api.parser()
session.add_argument('session_id', location='headers')

@ns.route('/send/<text>')
@api.doc(params={'text': 'The Text'})
@api.expect(session, bot)
class BotSearch(Resource):

    @ns.doc('send')
    def post(self, text):
        '''Send Text'''
        bot = BotDao(api.payload, session_id=request.headers.get('session_id'))
        search = bot.search(text)
        print(request.headers.get('session_id',))
        return search, 201

@ns.route('/article/<limit>')
@api.doc(params={'limit': 'The limit'})
@api.expect(session, bot)
class BotArticle(Resource):

    @ns.doc('send')
    def post(self, limit):
        '''Get article'''
        bot = BotDao(api.payload, session_id=request.headers.get('session_id'))
        article = bot.article(limit)
        print(request.headers.get('session_id',))
        return article, 201

@ns.route('/intro/')
@api.expect(session, bot)
class BotIntro(Resource):

    @ns.doc('send')
    def post(self):
        '''Get Intro'''
        bot = BotDao(api.payload, session_id=request.headers.get('session_id'))
        intro = bot.intro()
        print(request.headers.get('session_id',))
        return intro, 201

@ns.route('/error/')
@api.expect(session, bot)
class BotError(Resource):

    @ns.doc('send')
    def post(self):
        '''Get Error'''
        bot = BotDao(api.payload, session_id=request.headers.get('session_id'))
        intro = bot.key_error()
        print(request.headers.get('session_id',))
        return intro, 201

if __name__ == '__main__':
    app.run(debug=True)