Пример #1
0
 def accept_request(request):
     # Facebook recommends going through every entry since they might send
     # multiple messages in a single call during high load.
     for entry in request['entry']:
         for raw_message in entry['messaging']:
             ts_datetime = datetime.datetime.fromtimestamp(
                 int(raw_message['timestamp']) / 1000)
             crr_datetime = datetime.datetime.utcnow()
             diff = crr_datetime - ts_datetime
             if diff.total_seconds() < settings.GOLEM_CONFIG.get(
                     'MSG_LIMIT_SECONDS', 15):
                 # get and persist user and page ids
                 logging.debug(
                     'Incoming raw FB message: {}'.format(raw_message))
                 user_id = raw_message['sender']['id']
                 page_id = entry['id']
                 chat_id = FacebookInterface.create_chat_id(
                     page_id, user_id)
                 meta = {"user_id": user_id, "page_id": page_id}
                 session = ChatSession(FacebookInterface,
                                       chat_id,
                                       meta=meta)
                 FacebookInterface.fill_session_profile(session)
                 # Confirm accepted message
                 FacebookInterface.post_message(
                     session, SenderActionMessage('mark_seen'))
                 # Add it to the message queue
                 accept_user_message.delay(session.to_json(), raw_message)
             elif raw_message.get('timestamp'):
                 logging.warning(
                     "Delay {} too big, ignoring message!".format(diff))
Пример #2
0
    def accept_request(msg: WebMessageData):
        uid = str(msg.uid)

        logging.info('[WEBGUI] Received message from {}'.format(uid))
        session = ChatSession(WebGuiInterface, uid, meta={"uid": uid})
        accept_user_message.delay(session.to_json(),
                                  {"text": msg.message().get('text')})
Пример #3
0
    def accept_postback(msg: WebMessageData, data):
        uid = str(msg.uid)
        data = json.loads(data)

        logging.info('[WEBGUI] Received postback from {}'.format(uid))
        session = ChatSession(WebGuiInterface, uid, meta={"uid": uid})
        accept_user_message.delay(session.to_json(), {
            "_message_text": msg.message().get('text'),
            "payload": data
        })
Пример #4
0
 def accept_request(body):
     uid = body['user']['userId']
     chat_id = body['conversation']['conversationId']
     meta = {"uid": uid, "chat_id": chat_id}
     profile = Profile(uid, None, None)
     session = ChatSession(GoogleActionsInterface, chat_id, meta, profile)
     accept_user_message.delay(session.to_json(), body).get()
     # responses = GoogleActionsInterface.response_cache.get(session.chat_id)
     responses = get_redis().get(
         "response_for_{id}".format(id=session.chat_id))  # TODO
     return GoogleActionsInterface.convert_responses(
         session, responses.decode('utf8'))
Пример #5
0
 def accept_request(body, num_tries=1) -> bool:
     if body['type'] == 'message':
         uid = body['from']['id']
         chat_id = body['conversation']['id']
         get_redis().set('chat_message_id:{}'.format(chat_id),
                         body['id'])  # TODO
         MicrosoftInterface.set_base_url(chat_id, body['serviceUrl'])
         MicrosoftInterface.set_bot_id(chat_id, body['recipient']['id'])
         accept_user_message.delay(MicrosoftInterface.name,
                                   uid,
                                   body,
                                   chat_id=chat_id)
         return True
     return False
Пример #6
0
 def accept_request(request):
     # Facebook recommends going through every entry since they might send
     # multiple messages in a single call during high load.
     for entry in request['entry']:
         for raw_message in entry['messaging']:
             ts_datetime = datetime.datetime.fromtimestamp(
                 int(raw_message['timestamp']) / 1000)
             crr_datetime = datetime.datetime.utcnow()
             diff = crr_datetime - ts_datetime
             if diff.total_seconds() < settings.GOLEM_CONFIG.get(
                     'MSG_LIMIT_SECONDS'):
                 # print("MSG FB layer GET FB:")
                 print('INCOMING RAW FB MESSAGE: {}'.format(raw_message))
                 uid = FacebookInterface.fbid_to_uid(
                     entry['id'], raw_message['sender']['id'])
                 # Confirm accepted message
                 FacebookInterface.post_message(
                     uid, SenderActionMessage('mark_seen'))
                 # Add it to the message queue
                 accept_user_message.delay('facebook', uid, raw_message)
             elif raw_message.get('timestamp'):
                 print("Delay {} too big, ignoring message!".format(diff))
                 print(raw_message)
Пример #7
0
    def accept_request(body, num_tries=1) -> bool:
        if not body or 'update_id' not in body:
            logging.warning('Invalid message received: {}'.format(body))
            return False

        if 'message' in body:
            message = body['message']

            chat_id = message['chat']['id']
            uid = message['from']['id'] if 'from' in message else None  # null for group chats
            if uid and not TelegramInterface.has_message_expired(message):
                logging.debug('Adding message to queue')
                accept_user_message.delay(TelegramInterface.name, uid, body, chat_id=chat_id)
                return True
            else:
                logging.warning('No sender specified, ignoring message')
                return False
        elif 'callback_query' in body:
            callback_query = body['callback_query']
            query_id = callback_query['id']
            message = callback_query['message']
            if message:
                chat_id = message['chat']['id']
                message_id = message['message_id']
                TelegramInterface.answer_callback_query(query_id, chat_id, message_id)
            else:
                logging.error('No message in callback query, probably too old, ignoring.')
            if 'message' in callback_query:
                # unfortunately, there is no way to check the age of callback itself
                chat_id = callback_query['message']['chat']['id']
                uid = message['from']['id'] if 'from' in message else None
                accept_user_message.delay(TelegramInterface.name, uid, body, chat_id=chat_id)
                return True
        else:
            logging.warning('Unknown message type')
            return False