Beispiel #1
0
def setup_socketio(app: web.Application):

    # SEE https://github.com/miguelgrinberg/python-socketio/blob/v4.6.1/docs/server.rst#aiohttp
    # TODO: ujson to speed up?
    # TODO: client_manager= to socketio.AsyncRedisManager/AsyncAioPikaManager for horizontal scaling (shared sessions)
    sio = AsyncServer(async_mode="aiohttp", logger=log, engineio_logger=False)
    sio.attach(app)

    app[APP_CLIENT_SOCKET_SERVER_KEY] = sio
    handlers_utils.register_handlers(app, handlers)
def setup_socketio(app: web.Application):
    # ----------------------------------------------
    # TODO: temporary, just to check compatibility between
    # trafaret and pydantic schemas
    assert_valid_config(app)
    # ---------------------------------------------
    mgr = None
    sio = AsyncServer(async_mode="aiohttp", client_manager=mgr, logging=log)
    sio.attach(app)
    app[APP_CLIENT_SOCKET_SERVER_KEY] = sio
    handlers_utils.register_handlers(app, handlers)
Beispiel #3
0
    def aiohttp_server(self):
        sio = AsyncServer(async_mode="aiohttp")

        @sio.on(EVENT_USER_UTTERED)
        async def on_user_uttered(session_id: str, request: Any):
            if self.bot_messages_stack:
                messages = self.bot_messages_stack.pop(0)
                for message in messages:
                    await sio.emit(EVENT_BOT_UTTERED, message, room=session_id)

        app = web.Application()
        sio.attach(app)
        runner = web.AppRunner(app)
        return runner
Beispiel #4
0
async def api_server(
        game_app: game.GameApp, sio: AsyncServer,
        player_ranking_repository: PlayerRankingRepository) -> web.AppRunner:
    app = web.Application()
    sio.attach(app)

    logging.basicConfig(level=logging.DEBUG)
    setup_routes(app, sio, game_app, player_ranking_repository)

    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, "0.0.0.0", 8080)
    await site.start()
    return runner
Beispiel #5
0
    def blueprint(
        self, on_new_message: Callable[[UserMessage], Awaitable[Any]]
    ) -> Blueprint:
        # Workaround so that socketio works with requests from other origins.
        # https://github.com/miguelgrinberg/python-socketio/issues/205#issuecomment-493769183
        sio = AsyncServer(async_mode="sanic", cors_allowed_origins=[])
        socketio_webhook = SocketBlueprint(
            sio, self.socketio_path, "socketio_webhook", __name__
        )

        # make sio object static to use in get_output_channel
        self.sio = sio

        @socketio_webhook.route("/", methods=["GET"])
        async def health(_: Request) -> HTTPResponse:
            return response.json({"status": "ok"})

        @sio.on("connect", namespace=self.namespace)
        async def connect(sid: Text, _) -> None:
            logger.debug(f"User {sid} connected to socketIO endpoint.")

        @sio.on("disconnect", namespace=self.namespace)
        async def disconnect(sid: Text) -> None:
            logger.debug(f"User {sid} disconnected from socketIO endpoint.")

        @sio.on("session_request", namespace=self.namespace)
        async def session_request(sid: Text, data: Optional[Dict]):
            if data is None:
                data = {}
            if "session_id" not in data or data["session_id"] is None:
                data["session_id"] = uuid.uuid4().hex
            if self.session_persistence:
                sio.enter_room(sid, data["session_id"])
            await sio.emit("session_confirm", data["session_id"], room=sid)
            logger.debug(f"User {sid} connected to socketIO endpoint.")

        @sio.on(self.user_message_evt, namespace=self.namespace)
        async def handle_message(sid: Text, data: Dict) -> Any:
            output_channel = SocketIOOutput(sio, self.bot_message_evt)

            if self.session_persistence:
                if not data.get("session_id"):
                    raise_warning(
                        "A message without a valid session_id "
                        "was received. This message will be "
                        "ignored. Make sure to set a proper "
                        "session id using the "
                        "`session_request` socketIO event."
                    )
                    return
                sender_id = data["session_id"]
            else:
                sender_id = sid

            message = UserMessage(
                data["message"], output_channel, sender_id, input_channel=self.name()
            )
            await on_new_message(message)

        return socketio_webhook
Beispiel #6
0
    async def start(self):
        global sio
        sio = AsyncServer(async_mode='aiohttp')
        self.sio = sio

        @sio.event
        async def connect(sid, environ):
            global client_sid
            client_sid = sid
            await sio.emit('response', {'type': 'response'})

        self.enable_sync()
        sio.attach(self.app, socketio_path='ws')
        self.runner = web.AppRunner(self.app)
        await self.runner.setup()
        site = web.TCPSite(self.runner, 'localhost', 8080)
        await site.start()
    def blueprint(self, on_new_message):
        sio = AsyncServer(async_mode="sanic")
        socketio_webhook = SocketBlueprint(sio, self.socketio_path,
                                           "socketio_webhook", __name__)

        @socketio_webhook.route("/", methods=['GET'])
        async def health(request):
            return response.json({"status": "ok"})

        @sio.on('connect', namespace=self.namespace)
        async def connect(sid, environ):
            logger.debug("User {} connected to socketIO endpoint.".format(sid))
            print('Connected!')

        @sio.on('disconnect', namespace=self.namespace)
        async def disconnect(sid):
            logger.debug("User {} disconnected from socketIO endpoint."
                         "".format(sid))

        @sio.on('session_request', namespace=self.namespace)
        async def session_request(sid, data):
            print('This is sessioin request')

            if data is None:
                data = {}
            if 'session_id' not in data or data['session_id'] is None:
                data['session_id'] = uuid.uuid4().hex
            await sio.emit("session_confirm", data['session_id'], room=sid)
            logger.debug("User {} connected to socketIO endpoint."
                         "".format(sid))

        @sio.on('user_uttered', namespace=self.namespace)
        async def handle_message(sid, data):

            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt,
                                            data['message'])
            if data['message'] == "/get_started":
                message = data['message']
            else:
                ##receive audio
                received_file = 'output_' + sid + '.wav'

                urllib.request.urlretrieve(data['message'], received_file)
                path = os.path.dirname(__file__)

                fs, audio = wav.read("output_{0}.wav".format(sid))
                message = ds.stt(audio, fs)

                await sio.emit(self.user_message_evt, {"text": message},
                               room=sid)

            message_rasa = UserMessage(message,
                                       output_channel,
                                       sid,
                                       input_channel=self.name())
            await on_new_message(message_rasa)

        return socketio_webhook
    def blueprint(self, on_new_message=()):
        sio = AsyncServer(async_mode="sanic")
        socketio_webhook = SocketBlueprint(sio, self.socketio_path,
                                           "socketio_webhook", __name__)

        @socketio_webhook.route("/", methods=['GET'])
        async def health(request):
            return response.json({"status": "ok"})

        @sio.on('connect', namespace=self.namespace)
        async def connect(sid, environ):
            logger.debug("User {} connected to socketIO endpoint.".format(sid))
            print('Connected!')

        @sio.on('disconnect', namespace=self.namespace)
        async def disconnect(sid):
            logger.debug("User {} disconnected from socketIO endpoint."
                         "".format(sid))

        @sio.on('session_request', namespace=self.namespace)
        async def session_request(sid, data):
            print('This is sessioin request')

            if data is None:
                data = {}
            if 'session_id' not in data or data['session_id'] is None:
                data['session_id'] = uuid.uuid4().hex
            await sio.emit("session_confirm", data['session_id'], room=sid)
            logger.debug("User {} connected to socketIO endpoint."
                         "".format(sid))

        @sio.on('uservoice_uttered', namespace=self.namespace)
        async def handle_message(sid, data):
            logger.debug("reached here with data")
            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt,
                                            data['message'])
            if data['message'] == "/get_started":
                message = data['message']
            else:
                ##receive audio
                if data["isSpeech"] == True:
                    message = self.StFun(data['message'])
                else:
                    message = self.TsFun(data['message'])
                await sio.emit(self.bot_message_evt, message, room=sid)

            if message != "":
                # send request to the bot
                pass

            # message_rasa = UserMessage(message, output_channel, sid,
            #                       input_channel=self.name())
            # await on_new_message(message_rasa)

        return socketio_webhook
Beispiel #9
0
def main() -> None:
    sio = AsyncServer(cors_allowed_origins='*')
    database = Database('data.db')
    player_ranking_repository = PlayerRankingRepository(database)
    ranking_system = RankingSystem(player_ranking_repository)
    game_app = GameApp()
    game_app.on_game_result = ranking_system.calculate_ranking_points
    loop = asyncio.get_event_loop()
    loop.create_task(api_server(game_app, sio, player_ranking_repository))
    loop.run_until_complete(game_app.start_games())
    loop.close()
    def blueprint(self, on_new_message):
        # Workaround so that socketio works with requests from other origins.
        # https://github.com/miguelgrinberg/python-socketio/issues/205#issuecomment-493769183
        sio = AsyncServer(async_mode="sanic", cors_allowed_origins=[])
        socketio_webhook = SocketBlueprint(sio, self.socketio_path,
                                           "socketio_webhook", __name__)

        @socketio_webhook.route("/", methods=["GET"])
        async def health(request: Request):
            return response.json({"status": "ok"})

        @sio.on("connect", namespace=self.namespace)
        async def connect(sid, environ):
            logger.debug("User {} connected to socketIO endpoint.".format(sid))

        @sio.on("disconnect", namespace=self.namespace)
        async def disconnect(sid):
            logger.debug(
                "User {} disconnected from socketIO endpoint.".format(sid))

        @sio.on("session_request", namespace=self.namespace)
        async def session_request(sid, data):
            if data is None:
                data = {}
            if "session_id" not in data or data["session_id"] is None:
                data["session_id"] = uuid.uuid4().hex
            await sio.emit("session_confirm", data["session_id"], room=sid)
            logger.debug("User {} connected to socketIO endpoint.".format(sid))

        @sio.on(self.user_message_evt, namespace=self.namespace)
        async def handle_message(sid, data):
            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt)

            if self.session_persistence:
                if not data.get("session_id"):
                    logger.warning("A message without a valid sender_id "
                                   "was received. This message will be "
                                   "ignored. Make sure to set a proper "
                                   "session id using the "
                                   "`session_request` socketIO event.")
                    return
                sender_id = data["session_id"]
            else:
                sender_id = sid

            message = UserMessage(data["message"],
                                  output_channel,
                                  sender_id,
                                  input_channel=self.name(),
                                  metadata=data['customData'])
            await on_new_message(message)

        return socketio_webhook
Beispiel #11
0
    def blueprint(self, on_new_message):
        sio = AsyncServer(async_mode='sanic')
        socketio_webhook = SocketBlueprint(sio, self.socketio_path,
                                           'socketio_webhook', __name__)

        @socketio_webhook.route("/", methods=['GET'])
        async def health(request):
            return response.json({"status": "ok"})

        @sio.on('connect', namespace=self.namespace)
        async def connect(sid, environ):
            logger.debug("User {} connected to socketIO endpoint.".format(sid))

        @sio.on('disconnect', namespace=self.namespace)
        async def disconnect(sid):
            logger.debug("User {} disconnected from socketIO endpoint."
                         "".format(sid))

        @sio.on('session_request', namespace=self.namespace)
        async def session_request(sid, data):
            if data is None:
                data = {}
            if 'session_id' not in data or data['session_id'] is None:
                data['session_id'] = uuid.uuid4().hex
            await sio.emit("session_confirm", data['session_id'], room=sid)
            logger.debug("User {} connected to socketIO endpoint."
                         "".format(sid))

        @sio.on(self.user_message_evt, namespace=self.namespace)
        async def handle_message(sid, data):
            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt)

            if self.session_persistence:
                if not data.get("session_id"):
                    logger.warning("A message without a valid sender_id "
                                   "was received. This message will be "
                                   "ignored. Make sure to set a proper "
                                   "session id using the "
                                   "`session_request` socketIO event.")
                    return
                sender_id = data['session_id']
            else:
                sender_id = sid

            message = UserMessage(data['message'],
                                  output_channel,
                                  sender_id,
                                  input_channel=self.name())
            await on_new_message(message)

        return socketio_webhook
Beispiel #12
0
    def run(self):
        global sio
        global app

        if sio:
            return

        sio = AsyncServer(async_mode='sanic',
                          cors_allowed_origins=[],
                          allow_upgrades=True,
                          transports=['websocket'],
                          ping_interval=15,
                          ping_timeout=25,
                          engineio_logger=False)
        app = Sanic()
        sio.attach(app)

        @sio.on('funenc_socketio_client_msg')
        def mdias_event(sid, data):
            _logger.debug(
                "get mdias msg: {data} with sid {sid}".format(data=data, sid=sid))
            return model_env.deal_event(sid, data)

        @sio.event
        async def connect(sid, environ):
            return model_env.deal_user_connect(sid, environ)

        @sio.event
        async def disconnect(sid):
            model_env.deal_user_disconnect(sid)

        # 必需放在服务线程中, 否则会报utf8错误
        threading.Thread(target=self.start_client_event).start()
        port = 9080
        app.config['CORS_SUPPORTS_CREDENTIALS'] = True
        host = config['socket_io_host'] or '0.0.0.0'
        app.run(host=host, port=port, register_sys_signals=False)
Beispiel #13
0
def ws_init():
    global SIO
    SIO = AsyncServer(async_mode='tornado', logger=False, engineio_logger=None)
    # see https://github.com/miguelgrinberg/python-socketio/blob/master/examples/server/tornado/app.py
    SIO.register_namespace(ChatNamespace('/chat'))
    # See https://python-socketio.readthedocs.io/en/latest/server.html#namespaces
    if get_config().max_miners > 0:
        # Only register pool namespace if we want to run a pool
        SIO.register_namespace(PoolNamespace('/pool'))
Beispiel #14
0
    def blueprint(self, on_new_message):
        sio = AsyncServer(async_mode="sanic")
        socketio_webhook = SocketBlueprint(sio, self.socketio_path, 'socketio_webhook', __name__)

        @socketio_webhook.route("/", methods=['GET'])
        async def health(request: Request):
            return response.json({"status": "ok"})

        @sio.on('connect', namespace=self.namespace)
        async def connect(sid, environ):
            logger.debug("User {} connected to socketio endpoint.".format(sid))

        @sio.on('disconnect', namespace=self.namespace)
        async def disconnect(sid):
            logger.debug("User {} disconnected from socketio endpoint."
                         "".format(sid))

        @sio.on('session_request', namespace=self.namespace)
        async def session_request(sid, data):
            if data is None:
                data = {}
            if 'session_id' not in data or data['session_id'] is None:
                data['session_id'] = uuid.uuid4().hex
            await sio.emit("session_confirm", data['session_id'], room=sid)
            logger.debug("User {} connected to socketio endpoint."
                         "".format(sid))

        @sio.on(self.user_message_evt, namespace=self.namespace)
        async def handle_message(sid, data):
            output_channel = WebchatOutput(sio, sid, self.bot_message_evt)

            if self.session_persistence and ("session_id" not in data or data["session_id"] is None):
                logger.debug("A message without a valid sender_id was received")
            else:
                if 'customData' in data:
                    output_channel.set_custom_data(data['customData'])
                    if 'language' in data['customData']:
                        output_channel.set_language(data['customData']['language'])
                sender_id = data['session_id'] if self.session_persistence else sid
                message = UserMessage(data['message'], output_channel, sender_id,
                                      input_channel=self.name())
                await on_new_message(message)

        return socketio_webhook
Beispiel #15
0
    def blueprint(
            self, on_new_message: Callable[[UserMessage],
                                           Awaitable[Any]]) -> Blueprint:
        # Workaround so that socketio works with requests from other origins.
        # https://github.com/miguelgrinberg/python-socketio/issues/205#issuecomment-493769183
        sio = AsyncServer(async_mode="sanic", cors_allowed_origins=[])
        socketio_webhook = SocketBlueprint(sio, self.socketio_path,
                                           "socketio_webhook", __name__)

        # make sio object static to use in get_output_channel
        self.sio = sio
        """fatta da me"""
        @sio.on('my_event', namespace=self.namespace)
        async def ciallo(sid: Text, _) -> None:
            logger.debug("ho trovato un messaggio ciallo")

        @socketio_webhook.route("/", methods=["GET"])
        async def health(_: Request) -> HTTPResponse:
            return response.json({"status": "ok"})

        @sio.on("connect", namespace=self.namespace)
        async def connect(sid: Text, data: Dict) -> None:
            logger.debug(f"User {sid} connected to socketIO endpoint.")
            logger.debug("mi sono connesso")
            logger.debug(data)

            global user_ID
            if ("io=" + user_ID == data["HTTP_COOKIE"]):
                logger.debug("we1")
                sid = user_ID
            else:
                user_ID = sid
                logger.debug(user_ID)

            logger.debug("Ho ricevuto richiesta di sessione")
            logger.debug(data["HTTP_COOKIE"])
            if data is None:
                data = {}
            if "session_id" not in data or data["session_id"] is None:
                data["session_id"] = uuid.uuid4().hex
            if self.session_persistence:
                sio.enter_room(sid, data["session_id"])
            await sio.emit("session_confirm", data["session_id"], room=sid)
            logger.debug(f"User {sid} session requested to socketIO endpoint.")

            if (tracker == True):
                data = {}
                data['names'] = []
                data['names'].append({
                    'sender':
                    'bot',
                    'message':
                    'What data are you looking for?',
                })

                with open("sessions/" + sid + ".json", "w") as f:
                    json.dump(data, f)

            await self.sio.emit(
                'json_response', {
                    "type": "message",
                    "payload": {
                        "sender": "bot",
                        "text": "What data are you looking for?"
                    }
                })
            await self.sio.emit(
                'json_response', {
                    "type": "available_choices",
                    "payload": {
                        "showSearchBar":
                        False,
                        "showDetails":
                        False,
                        "caption":
                        'Data available',
                        "showHelpIcon":
                        False,
                        "elements": [{
                            'name': 'Annotations',
                            'value': 'Annotations'
                        }, {
                            'name': 'Experiments',
                            'value': 'Experiments'
                        }]
                    }
                })
            await self.sio.emit('json_response', {
                "type": "workflow",
                "payload": {
                    "state": "Data Selection"
                }
            })

        @sio.on("disconnect", namespace=self.namespace)
        async def disconnect(sid: Text) -> None:
            logger.debug(f"User {sid} disconnected from socketIO endpoint.")

        @sio.on("reconnect", namespace=self.namespace)
        async def reconnect(sid: Text) -> None:
            print("i'm back online")
            logger.debug(" i'm back")

        @sio.on("session_request", namespace=self.namespace)
        async def session_request(sid: Text, data: Optional[Dict]):
            logger.debug("Ho ricevuto richiesta di sessione")
            if data is None:
                data = {}
            if "session_id" not in data or data["session_id"] is None:
                data["session_id"] = uuid.uuid4().hex
            if self.session_persistence:
                sio.enter_room(sid, data["session_id"])
            await sio.emit("session_confirm", data["session_id"], room=sid)
            logger.debug("User {sid} session requested to socketIO endpoint.")

        @sio.on(self.user_message_evt, namespace=self.namespace)
        async def handle_message(sid: Text, data: Dict) -> Any:
            output_channel = SocketIOOutput(sio, self.bot_message_evt)
            if self.session_persistence:
                if not data.get("session_id"):
                    rasa.shared.utils.io.raise_warning(
                        "A message without a valid session_id "
                        "was received. This message will be "
                        "ignored. Make sure to set a proper "
                        "session id using the "
                        "`session_request` socketIO event.")
                    return
                sender_id = data["session_id"]
            else:
                sender_id = sid

            message = UserMessage(data["message"],
                                  output_channel,
                                  sender_id,
                                  input_channel=self.name())
            await on_new_message(message)

        """fatta da me da é questo che funziona veramente"""

        @sio.on('my_event', namespace=self.namespace)
        async def handle_message(sid: Text, data: Dict) -> Any:
            output_channel = SocketIOOutput(sio, self.bot_message_evt)
            logger.debug("ho preso un messaggio fatto da me rimuovere 1")

            global user_ID

            message = UserMessage(data["data"],
                                  output_channel,
                                  user_ID,
                                  input_channel=self.name())
            print('data[data]', data["data"])
            with open('data.txt', 'a') as outfile:
                json.dump(data["data"] + '\n', outfile)

            if (tracker == True):
                with open("sessions/" + user_ID + ".json") as json_file:
                    data2 = json.load(json_file)
                    temp = data2['names']
                    y = {"sender": user_ID, 'message': data["data"]}
                    temp.append(y)

                write_json(data2, "sessions/" + user_ID + ".json")

            await on_new_message(message)

        return socketio_webhook
Beispiel #16
0
from socketio import AsyncServer

sio = AsyncServer(async_mode="sanic")
socketio_webhook = SocketBlueprint(
            sio, self.socketio_path, "socketio_webhook", __name__
        )
Beispiel #17
0
import uuid
from socketio import AsyncServer
from sanic.log import logger

from slack_integration import SlackIntegration, session_to_channel

session_to_sid = {}

ERROR_CONNECT_TO_SLACK = "Could not connect to operator"
ERROR_SEND_MESSAGE = "Could not send message"

sio = AsyncServer(async_mode="sanic", cors_allowed_origins=[])  # '*'
conn = SlackIntegration()


@sio.on("connect")
async def connect(sid, environ):
    logger.info(f"User {sid} connected to socketIO endpoint.")


@sio.on("disconnect")
async def disconnect(sid):
    logger.info(f"User {sid} disconnected from socketIO endpoint.")


@sio.on("session_request")
async def session_request(sid, data):
    if data is None:
        data = {}
    if "session_id" not in data or data["session_id"] is None:
        data["session_id"] = uuid.uuid4().hex
Beispiel #18
0
    def async_server(self, container):
        engineio_options = container('config', namespace=self.namespace)

        return AsyncServer(**engineio_options)
Beispiel #19
0
    def blueprint(self, on_new_message):
        sio = AsyncServer(async_mode="sanic",
                          logger=True,
                          cors_allowed_origins='*')
        socketio_webhook = SocketBlueprint(sio, self.socketio_path,
                                           "socketio_webhook", __name__)

        @socketio_webhook.route("/", methods=['GET'])
        async def health(request):
            return response.json({"status": "ok"})

        @sio.on('connect')
        async def connect(sid, environ):
            print("User {} connected to socketIO endpoint.".format(sid))

        @sio.on('disconnect')
        async def disconnect(sid):
            print("User {} disconnected from socketIO endpoint."
                  "".format(sid))

        @sio.on('session_request')
        async def session_request(sid, data):
            print('Session request received')

            if data is None:
                data = {}
            if 'session_id' not in data or data['session_id'] is None:
                data['session_id'] = uuid.uuid4().hex
            await sio.emit("session_confirm", data['session_id'], room=sid)
            print("User {} connected to socketIO endpoint." "".format(sid))

        @sio.on('user_uttered')
        async def handle_message(sid, data):
            print('User uttered')
            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt,
                                            data['message'])
            if data['message'] == "/get_started":
                message = data['message']
            else:
                ##receive audio
                received_file = 'output_' + sid + '.wav'

                request.urlretrieve(data['message'], received_file)

                # fs, audio = wav.read("output_{0}.wav".format(sid))
                input_audio_file = wave.open("output_{0}.wav".format(sid),
                                             'rb')
                converted_audio_to_bytes = numpy.frombuffer(
                    input_audio_file.readframes(input_audio_file.getnframes()),
                    numpy.int16)
                input_audio_file.close()
                message = self.speech_to_text_model.stt(
                    converted_audio_to_bytes)

                insert_user_message_to_database(message, sid)

                await sio.emit(self.user_message_evt, {"text": message},
                               room=sid)

            message_rasa = UserMessage(message,
                                       output_channel,
                                       sid,
                                       input_channel=self.name())
            await on_new_message(message_rasa)

        return socketio_webhook
Beispiel #20
0
from socketio import AsyncServer
from aiohttp import web
from aiohttp_session import get_session, setup
from aiohttp_session.cookie_storage import EncryptedCookieStorage
from hashlib import sha3_512
from redis import Redis
import pickle

database = Redis(host='localhost', port=6379, db=0)

app = web.Application()
setup(app, EncryptedCookieStorage(b'ghnrty1twoo7234th66ytewtke3arer&'))
sio = AsyncServer(async_mode='aiohttp')
sio.attach(app)

routes = web.RouteTableDef()


@routes.get("/")
async def index_route(request):
    with open('index.html', 'rb') as file:
        index = file.read()
        return web.Response(body=index, content_type="text/html")


@routes.post("/app")
async def app_route(request):
    session = await get_session(request)
    data = await request.post()
    if 'secret' in data:
        code = sha3_512()
    def blueprint(self, on_new_message):
        sio = AsyncServer(async_mode="sanic")
        socketio_webhook = SocketBlueprint(sio, self.socketio_path,
                                           "socketio_webhook", __name__)

        @socketio_webhook.route("/", methods=['GET'])
        async def health(request):
            return response.json({"status": "ok"})

        @sio.on('connect', namespace=self.namespace)
        async def connect(sid, environ):
            logger.debug("User {} connected to socketIO endpoint.".format(sid))
            print('Connected!')

        @sio.on('disconnect', namespace=self.namespace)
        async def disconnect(sid):
            logger.debug("User {} disconnected from socketIO endpoint."
                         "".format(sid))

        @sio.on('session_request', namespace=self.namespace)
        async def session_request(sid, data):
            print('This is sessioin request')
            #print(data)
            #print(data['session_id'])
            if data is None:
                data = {}
            if 'session_id' not in data or data['session_id'] is None:
                data['session_id'] = uuid.uuid4().hex
            await sio.emit("session_confirm", data['session_id'], room=sid)
            logger.debug("User {} connected to socketIO endpoint."
                         "".format(sid))

        #@sio.on('recorder stopped', namespace=self.namespace)
        #async def get_audio(sid, data):
        #    print('This is what I got')
        #    print(data)

        @sio.on('user_uttered', namespace=self.namespace)
        async def handle_message(sid, data):

            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt,
                                            data['message'])
            if data['message'] == "/get_started":
                message = data['message']
            else:
                ##receive audio as .ogg
                received_file = sid + '.wav'

                urllib.request.urlretrieve(data['message'], received_file)
                path = os.path.dirname(__file__)
                #print(path)
                #print(sid)
                # convert .ogg file into int16 wave file by ffmpeg
                #-ar 44100
                os.system("ffmpeg -y -i {0} -ar 16000 output_{1}.wav".format(
                    received_file, sid))
                #os.system("ffmpeg -y -i {0} -c:a pcm_s161e output_{1}.wav".format(received_file,sid))
                N_FEATURES = 25
                N_CONTEXT = 9
                BEAM_WIDTH = 500
                LM_ALPHA = 0.75
                LM_BETA = 1.85

                ds = Model('deepspeech-0.5.1-models/output_graph.pbmm',
                           N_FEATURES, N_CONTEXT,
                           'deepspeech-0.5.1-models/alphabet.txt', BEAM_WIDTH)
                fs, audio = wav.read("output_{0}.wav".format(sid))
                message = ds.stt(audio, fs)

                #await self.sio.emit(self.bot_message_evt, response, room=socket_id)
                await sio.emit("user_uttered", {"text": message}, room=sid)
                #ffmpeg -i input.flv -f s16le -acodec pcm_s16le output.raw

            if self.session_persistence:
                #if not data.get("session_id"):
                #    logger.warning("A message without a valid sender_id "
                #                   "was received. This message will be "
                #                   "ignored. Make sure to set a proper "
                #                   "session id using the "
                #                   "`session_request` socketIO event.")
                #    return
                #sender_id = data['session_id']
                #else:
                sender_id = sid

            message_rasa = UserMessage(message,
                                       output_channel,
                                       sender_id,
                                       input_channel=self.name())
            await on_new_message(message_rasa)

        return socketio_webhook
Beispiel #22
0
    def blueprint(self, on_new_message):
        sio = AsyncServer(async_mode="sanic",
                          cors_allowed_origins=self.cors_allowed_origins)
        socketio_webhook = SocketBlueprint(sio, self.socketio_path,
                                           "socketio_webhook", __name__)

        @socketio_webhook.route("/", methods=["GET"])
        async def health(request: Request):
            return response.json({"status": "ok"})

        @sio.on("connect", namespace=self.namespace)
        async def connect(sid, environ):
            ip = environ['REMOTE_ADDR'] or environ['HTTP_X_FORWARDED_FOR']
            self.remote_addr = ip
            self.c = MyUtils.get_record_db_cursor()
            logger.debug(
                "User {} (from {}) connected to socketIO endpoint.".format(
                    sid, ip))

        @sio.on("disconnect", namespace=self.namespace)
        async def disconnect(sid):

            self.c.connection.close()
            logger.debug(
                "User {} disconnected from socketIO endpoint.".format(sid))

        @sio.on("session_request", namespace=self.namespace)
        async def session_request(sid, data):
            if data is None:
                data = {}
            if "session_id" not in data or data["session_id"] is None:
                data["session_id"] = uuid.uuid4().hex
            await sio.emit("session_confirm", data["session_id"], room=sid)
            logger.debug("User {} connected to socketIO endpoint.".format(sid))

        @sio.on(self.user_message_evt, namespace=self.namespace)
        async def handle_message(sid, data):
            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt)
            logger.debug("Received data: {} ".format(data))

            if self.session_persistence:
                if not data.get("session_id"):
                    logger.warning("A message without a valid sender_id "
                                   "was received. This message will be "
                                   "ignored. Make sure to set a proper "
                                   "session id using the "
                                   "`session_request` socketIO event.")
                    return
                sender_id = data["session_id"]
            else:
                sender_id = sid
            message = UserMessage(data["message"],
                                  output_channel,
                                  sender_id,
                                  input_channel=self.name())
            if data["message"][0] != '/':
                query = """insert into message_received
                    (from_user_id, session_id, content, `when`, ip_address)
                    values (%s, %s, %s, %s, %s)"""
                self.c.execute(
                    query, (data["customData"]["from_user_id"],
                            data["session_id"], data["message"],
                            time.strftime("%Y-%m-%d %H:%M:%S",
                                          time.localtime()), self.remote_addr))
                self.c.connection.commit()
            await on_new_message(message)

        return socketio_webhook
Beispiel #23
0
    def blueprint(
        self, on_new_message: Callable[[UserMessage], Awaitable[Any]]
    ) -> Blueprint:
        # Workaround so that socketio works with requests from other origins.
        # https://github.com/miguelgrinberg/python-socketio/issues/205#issuecomment-493769183
        sio = AsyncServer(
            async_mode="sanic", cors_allowed_origins=self.cors_allowed_origins
        )
        socketio_webhook = SocketBlueprint(
            sio, self.socketio_path, "socketio_webhook", __name__
        )

        # make sio object static to use in get_output_channel
        self.sio = sio

        @socketio_webhook.route("/", methods=["GET"])
        async def health(_: Request) -> HTTPResponse:
            return response.json({"status": "ok"})

        @sio.on("connect", namespace=self.namespace)
        async def connect(sid: Text, _) -> None:
            logger.debug(f"User {sid} connected to socketIO endpoint.")

        @sio.on("disconnect", namespace=self.namespace)
        async def disconnect(sid: Text) -> None:
            logger.debug(f"User {sid} disconnected from socketIO endpoint.")

        @sio.on("session_request", namespace=self.namespace)
        async def session_request(sid: Text, data: Optional[Dict]):
            props = {}
            if data is None:
                data = {}
            if "session_id" not in data or data["session_id"] is None:
                data["session_id"] = uuid.uuid4().hex
            if self.session_persistence:
                sio.enter_room(sid, data["session_id"])
            if self.config is not None:
                props = self.config
            else:
                config = await get_config_via_graphql(
                    os.environ.get("BF_URL"), os.environ.get("BF_PROJECT_ID")
                )
                if config and "credentials" in config:
                    credentials = config.get("credentials", {})
                    channel = credentials.get("rasa_addons.core.channels.webchat_plus.WebchatPlusInput")
                    if channel is None: channel = credentials.get("rasa_addons.core.channels.WebchatPlusInput")
                    if channel is None: channel = credentials.get("rasa_addons.core.channels.webchat.WebchatInput")
                    if channel is None: channel = credentials.get("rasa_addons.core.channels.WebchatInput", {})
                    props = channel.get("props", {})

            await sio.emit(
                "session_confirm",
                {"session_id": data["session_id"], "props": props},
                room=sid,
            )
            logger.debug(f"User {sid} connected to socketIO endpoint.")

        @sio.on(self.user_message_evt, namespace=self.namespace)
        async def handle_message(sid: Text, data: Dict) -> Any:
            output_channel = WebchatOutput(sio, self.bot_message_evt)

            if self.session_persistence:
                if not data.get("session_id"):
                    warnings.warn(
                        "A message without a valid sender_id "
                        "was received. This message will be "
                        "ignored. Make sure to set a proper "
                        "session id using the "
                        "`session_request` socketIO event."
                    )
                    return
                sender_id = data["session_id"]
            else:
                sender_id = sid

            message = UserMessage(
                data["message"],
                output_channel,
                sender_id,
                input_channel=self.name(),
                metadata=self.get_metadata(data),
            )
            await on_new_message(message)

        return socketio_webhook
Beispiel #24
0
    def blueprint(self, on_new_message):
        sio = AsyncServer(async_mode="sanic")
        socketio_webhook = SocketBlueprint(sio, self.socketio_path,
                                           "socketio_webhook", __name__)
        from tinydb import TinyDB, Query
        import message_handler
        db_users = TinyDB('db_users.json')
        User = Query()

        @socketio_webhook.route("/", methods=["GET"])
        async def health(request: Request):
            return response.json({"status": "ok"})

        @sio.on("connect", namespace=self.namespace)
        async def connect(sid, environ):
            logger.debug("User {} connected to socketIO endpoint.".format(sid))

        @sio.on("disconnect", namespace=self.namespace)
        async def disconnect(sid):
            logger.debug(
                "User {} disconnected from socketIO endpoint.".format(sid))

        @sio.on("session_request", namespace=self.namespace)
        async def session_request(sid, data):
            print("request made to connect")
            print(data)
            if data is None:
                data = {}
            if "session_id" not in data or data["session_id"] is None:
                print("creation d'un session id")
                data["session_id"] = uuid.uuid4().hex
            await sio.emit("session_confirm", data["session_id"], room=sid)
            logger.debug("User {} connected to socketIO endpoint.".format(sid))

        @sio.on(self.user_message_evt, namespace=self.namespace)
        async def handle_message(sid, data):
            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt)
            print(data)
            #await output_channel._send_message(sid,{"text": "__ /!\ Enter the passphrase to use the chatbot :__"})
            #await message_handler.handle_message(data, db_users, db_users.search(User.userName == data['customData']['userName']), output_channel, sid)
            ''' if( data["message"] == "%/get_started"):      
                message = {"text": "__Welcome to the chatbot testing page, please enter provided passphrase to access functionalities__", "quick_replies": []}
                message["quick_replies"].append({
                    "content_type": "text",
                    "title": "English",
                    "payload": "/English",
                })
                message["quick_replies"].append({
                    "content_type": "text",
                    "title": "French",
                    "payload": "/French",
                })
                # await output_channel._send_message(sid,message)
            else:
                await output_channel._send_message(sid,{"text": "__ /!\ Enter the passphrase to use the chatbot :__"})'''

            if self.session_persistence:
                if not data.get("session_id"):
                    logger.warning("A message without a valid sender_id "
                                   "was received. This message will be "
                                   "ignored. Make sure to set a proper "
                                   "session id using the "
                                   "`session_request` socketIO event.")
                    return
                sender_id = data["session_id"]
            else:
                sender_id = sid
                logger.info("message reçu")

            message = UserMessage(data["message"],
                                  output_channel,
                                  sender_id,
                                  input_channel=self.name())
            await on_new_message(message)
            print(message)

        return socketio_webhook
Beispiel #25
0
from aiohttp import web
from socketio import AsyncServer
import json
import os
import time
from threading import Thread

import constants as c
import UsrDefClasses
import StateMachines as sM

# ------------------------------------------------
# Variables
# ------------------------------------------------

sio = AsyncServer()
app = web.Application()
sio.attach(app)
AudioCtrl = UsrDefClasses.AudioControls()
PowerCtrl = UsrDefClasses.PowerControls()
MediaCtrl = UsrDefClasses.MediaControls()
NAS = UsrDefClasses.Nas()
StateMachine = sM.StateMachine(PowerCtrl, AudioCtrl, MediaCtrl)
appRoutes = c.ROUTES
iNetRadioJson = list()
link = c.DEFAULTAUDIOFILE
title = c.DEFAULTAUDIOTITLE

# ------------------------------------------------
# Functions/ events
# ------------------------------------------------
Beispiel #26
0
    def blueprint(self, on_new_message):
        sio = AsyncServer(async_mode="sanic", cors_allowed_origins="*")
        socketio_webhook = SocketBlueprint(sio, self.socketio_path,
                                           "socketio_webhook", __name__)

        @socketio_webhook.route("/", methods=['GET'])
        async def health(request):
            return response.json({"status": "ok"})

        @sio.on('connect', namespace=self.namespace)
        async def connect(sid, environ):
            logger.debug("User {} connected to socketIO endpoint.".format(sid))
            print('Connected!')

        def read_wav_file(self, fileName):
            with wave.open(fileName, 'rb') as w:
                rate = w.getframerate()
                frames = w.getnframes()
                buffer = w.readframes(frames)
                print(rate)
                print(frames)
            return buffer, rate

        @sio.on('disconnect', namespace=self.namespace)
        async def disconnect(sid):
            logger.debug("User {} disconnected from socketIO endpoint."
                         "".format(sid))

        @sio.on('session_request', namespace=self.namespace)
        async def session_request(sid, data):
            print('This is sessioin request')

            if data is None:
                data = {}
            if 'session_id' not in data or data['session_id'] is None:
                data['session_id'] = uuid.uuid4().hex
            await sio.emit("session_confirm", data['session_id'], room=sid)
            logger.debug("User {} connected to socketIO endpoint."
                         "".format(sid))

        def speech_to_text(WAVE_OUTPUT_FILENAME):
            r = sr.Recognizer()

            # open the file
            with sr.AudioFile(WAVE_OUTPUT_FILENAME) as source:
                # listen for the data (load audio to memory)
                audio_data = r.record(source)
                # recognize (convert from speech to text)
                text = r.recognize_google(audio_data)
                return text

        @sio.on('user_uttered', namespace=self.namespace)
        async def handle_message(sid, data):

            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt,
                                            data['message'])
            if data['message'] == "/get_started":
                message = data['message']
            else:
                ##receive audio
                received_file = 'output_' + sid + '.wav'

                urllib.request.urlretrieve(data['message'], received_file)

                message = speech_to_text(received_file)
                await sio.emit(self.user_message_evt, {"text": message},
                               room=sid)

            message_rasa = UserMessage(message,
                                       output_channel,
                                       sid,
                                       input_channel=self.name())
            await on_new_message(message_rasa)

        return socketio_webhook
Beispiel #27
0
    def blueprint(self, on_new_message):
        sio = AsyncServer(async_mode="sanic", cors_allowed_origins='*')
        socketio_webhook = SocketBlueprint(
            sio, self.socketio_path, "socketio_webhook", __name__
        )

        @socketio_webhook.route("/", methods=['GET'])
        async def health(request):
            return response.json({"status": "ok"})

        @sio.on('connect', namespace=self.namespace)
        async def connect(sid, environ):
            logger.debug("User {} connected to socketIO endpoint.".format(sid))

        @sio.on('disconnect', namespace=self.namespace)
        async def disconnect(sid):
            logger.debug("User {} disconnected from socketIO endpoint."
                         "".format(sid))

        @sio.on('session_request', namespace=self.namespace)
        async def session_request(sid, data):
            if data is None:
                data = {}
            if 'session_id' not in data or data['session_id'] is None:
                data['session_id'] = uuid.uuid4().hex
            await sio.emit("session_confirm", data['session_id'], room=sid)
            logger.debug("User {} connected to socketIO endpoint."
                         "".format(sid))

        @sio.on('user_uttered', namespace=self.namespace)
        async def handle_message(sid, data):

            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt, data['message'])
            if data['message'].startswith("data:audio"):
                try:  # message is an audio file
                    received_file = 'output_rune.wav'
                    audiopath = get_path_from_filename(received_file)
                    logger.info(audiopath)
                    urllib.request.urlretrieve(data['message'], audiopath)
                    import os.path
                    if not os.path.isfile(audiopath):
                        raise Exception

                    if USE_STT:
                        message = generate_text(received_file, method=USE_STT)
                    else:
                        message = "Speech to Text isn't available"
                    logger.info('STT ({}): {}'.format(USE_STT, message))
                except Exception as e:  # message is a string
                    logger.error(e)
                    message = None
            else:
                message = data["message"].lower()
                logger.info('TXT: {}'.format(message))

            if message is not None:
                await sio.emit(self.user_message_evt, {"text": message.casefold()}, room=sid)

                message_rasa = UserMessage(message, output_channel, sid,
                                        input_channel=self.name())
                await on_new_message(message_rasa)
            else:
                await sio.emit(self.bot_message_evt, {'text': "Dude, I can't hear shit, could you try speaking English?", 'link': None}, room=sid)

        return socketio_webhook
    def blueprint(
            self, on_new_message: Callable[[UserMessage],
                                           Awaitable[Any]]) -> Blueprint:
        # Workaround so that socketio works with requests from other origins.
        # https://github.com/miguelgrinberg/python-socketio/issues/205#issuecomment-493769183
        sio = AsyncServer(async_mode="sanic", cors_allowed_origins=[])
        socketio_webhook = SocketVoiceBlueprint(sio, self.socketio_path,
                                                "socketio_webhook", __name__)

        # make sio object static to use in get_output_channel
        self.sio = sio

        @socketio_webhook.route("/", methods=["GET"])
        async def health(_: Request) -> HTTPResponse:
            return response.json({"status": "ok"})

        @sio.on("connect", namespace=self.namespace)
        async def connect(sid: Text, _) -> None:
            logger.debug(f"User {sid} connected to socketIO endpoint.")

        @sio.on("disconnect", namespace=self.namespace)
        async def disconnect(sid: Text) -> None:
            logger.debug(f"User {sid} disconnected from socketIO endpoint.")

        @sio.on("session_request", namespace=self.namespace)
        async def session_request(sid: Text, data: Optional[Dict]):
            if data is None:
                data = {}
            if "session_id" not in data or data["session_id"] is None:
                data["session_id"] = uuid.uuid4().hex
            if self.session_persistence:
                sio.enter_room(sid, data["session_id"])
            await sio.emit("session_confirm", data["session_id"], room=sid)
            logger.debug(f"User {sid} connected to socketIO endpoint.")

        @sio.on(self.user_message_evt, namespace=self.namespace)
        async def handle_message(sid: Text, data: Dict) -> Any:
            output_channel = SocketIOVoiceOutput(sio, self.bot_message_evt,
                                                 self.botium_speech_url,
                                                 self.botium_speech_apikey,
                                                 self.botium_speech_language,
                                                 self.botium_speech_voice)

            if self.session_persistence:
                if not data.get("session_id"):
                    rasa.shared.utils.io.raise_warning(
                        "A message without a valid session_id "
                        "was received. This message will be "
                        "ignored. Make sure to set a proper "
                        "session id using the "
                        "`session_request` socketIO event.")
                    return
                sender_id = data["session_id"]
            else:
                sender_id = sid

            if data['message'].startswith('data:'):
                header, encoded = data['message'].split(",", 1)

                audioData = base64.b64decode(encoded.encode('ascii'))

                convertEndpoint = f"{self.botium_speech_url}/api/convert/WAVTOMONOWAV"
                logger.debug(
                    f"handle_message - Calling Convert Endpoint: {convertEndpoint}"
                )
                res = urlopen(
                    Request(url=convertEndpoint,
                            data=audioData,
                            method='POST',
                            headers={'content-type': 'audio/wav'}))
                audioDataWav = res.read()

                #with open('decoded_image.wav', 'wb') as file_to_save:
                #    file_to_save.write(audioData)

                audioEndpoint = f"{self.botium_speech_url}/api/stt/{self.botium_speech_language}"
                logger.debug(
                    f"handle_message - Calling Speech Endpoint: {audioEndpoint}"
                )
                res = urlopen(
                    Request(url=audioEndpoint,
                            data=audioDataWav,
                            method='POST',
                            headers={'content-type': 'audio/wav'}))
                resJson = json.loads(res.read().decode('utf-8'))
                logger.debug(
                    f"handle_message - Calling Speech Endpoint: {audioEndpoint} => {resJson}"
                )
                message = resJson["text"]

                await sio.emit(self.user_message_evt, {"text": message},
                               room=sid)
            else:
                message = data['message']

            message = UserMessage(message,
                                  output_channel,
                                  sender_id,
                                  input_channel=self.name())
            await on_new_message(message)

        return socketio_webhook
Beispiel #29
0
        except HTTPException:
            await self.disconnect(sid, namespace=self.namespace)
            print("##### HTTPException ######")
        except ValidationError:
            await self.emit(socket_events.server_error,
                            ServerResponse(message="packet validation error", type="validation_error").dict(),
                            room=sid)
            print("##### ValidationError ######")
        except:
            await self.emit(socket_events.server_error,
                            ServerResponse(message="something went wrong", type="error").dict(),
                            room=sid)
            print("##### General Error ######")


sio = AsyncServer(async_mode='asgi')
sio.register_namespace(ChatNameSpace('/chat'))
socket_app = ASGIApp(sio)


@sio.on("my event")
async def my_event(sid, data):
    print("*#*#*#*#*#*")


class HeadersModel(BaseModel):
    HTTP_TOKEN: str


def get_headers(data: tuple) -> HeadersModel:
    data: dict = data[0]
Beispiel #30
0
    'VOL-L-L': 'e',
    'VOL-L-R': 'r',
    'VOL-R-L': 'i',
    'VOL-R-R': 'o'
}

import asyncio
from sanic import Sanic
from sanic_jinja2 import SanicJinja2
from socketio import AsyncServer
from datetime import datetime
from time import mktime
import pyautogui
pyautogui.FAILSAFE = False

sio = AsyncServer(async_mode='sanic')
app = Sanic(__name__)
sio.attach(app)
jinja = SanicJinja2(app)


def millis_interval(start, end):
    """start and end are datetime instances"""
    diff = end - start
    millis = diff.days * 24 * 60 * 60 * 1000
    millis += diff.seconds * 1000
    millis += diff.microseconds / 1000
    return millis


@app.route('/')