Ejemplo n.º 1
0
def send_text():
    """Receive text via Twilio"""
    
    contact_phone = request.values.get("From", None)
    response_message = request.values.get("Body", None)

    message_status = request.values.get("SmsStatus", None)

    if contact_phone != None:
        contact_phone = contact_phone[2:]
        contacts = Contact.query.filter_by(contact_phone=contact_phone).all()
        contact_id_list = []

        for contact in contacts:
            contact_id_list.append(contact.contact_id)
        msg_id = db.session.query(func.max(MessageContact.message_id)).filter(MessageContact.contact_id.in_(contact_id_list)).one()
        msg = Message.query.filter_by(message_id=msg_id).one()
        user = msg.user
        user_phone = user.phone_number
        to_lang = user.language.yandex_lang_code
        # all contacts that belong to that user and get a first one that matches the phone number
        from_contact = Contact.query.filter((Contact.user_id==user.user_id) & (Contact.contact_phone==contact_phone)).first()
        from_lang = from_contact.language.yandex_lang_code
        translated_resp_msg = yandex.translate_message(response_message, from_lang, to_lang)['text']
        user_messages = Message.query.count()
        if user_messages < 2000:
            twilio_api.send_message(translated_resp_msg, user_phone)

        return ('', 204)

    else:
        render_template('home.html')
Ejemplo n.º 2
0
    def get_eng_msg(self):
        """ Message object -> string
        Returns an original msg but in english"""
        import yandex

        if self.original_lang_id == 1:
    
            return self.message_text

        trans_eng_msg = MessageLang.query.filter((MessageLang.lang_id==1) & (MessageLang.message_id==self.message_id)).first()
        
        if trans_eng_msg:
            
            return trans_eng_msg.translated_message

        else:

            translate_to_eng = yandex.translate_message(self.message_text, self.language.yandex_lang_code, 'en')
            eng_msg = ''.join(translate_to_eng['text'])

            fake_eng_msg = MessageLang(translated_message=eng_msg, message_status=270, lang_id=1, message_id=self.message_id)

            db.session.add(fake_eng_msg)
            db.session.commit()
            
            return eng_msg

        return ""
Ejemplo n.º 3
0
    def get_eng_msg(self):
        """ Message object -> string
        Returns an original msg but in english"""
        import yandex

        if self.original_lang_id == 1:

            return self.message_text

        trans_eng_msg = MessageLang.query.filter((MessageLang.lang_id == 1) & (
            MessageLang.message_id == self.message_id)).first()

        if trans_eng_msg:

            return trans_eng_msg.translated_message

        else:

            translate_to_eng = yandex.translate_message(
                self.message_text, self.language.yandex_lang_code, 'en')
            eng_msg = ''.join(translate_to_eng['text'])

            fake_eng_msg = MessageLang(translated_message=eng_msg,
                                       message_status=270,
                                       lang_id=1,
                                       message_id=self.message_id)

            db.session.add(fake_eng_msg)
            db.session.commit()

            return eng_msg

        return ""
Ejemplo n.º 4
0
    def on_user_message(self, trans_msg):
        """Translate a message and send it to the room"""

        user1 = User.query.filter_by(user_id=trans_msg['user1']).one()
        user2 = User.query.filter_by(user_id=trans_msg['user2']).one()

        trans_msg = yandex.translate_message(trans_msg['message'], 
                                        user1.language.yandex_lang_code, 
                                        user2.language.yandex_lang_code)['text']

        trans_msg = ''.join(trans_msg)

        room = self.room_name(user1.user_id, user2.user_id)
        self.emit_to_room(room, 'msg_to_room', 
                        self.socket.session['nickname'], 
                        trans_msg)
def translate_unique_langs(unique_lang_dict, user_message, lang_code, message_id, add_msg):
    """ dict, string, string, int, bool  -> dict with key as string & value-string
    Translate unique langs"""

    trans_msgs_dict = {}
    for unique_lang_id in unique_lang_dict:
        unique_lang_code = unique_lang_dict[unique_lang_id]
        trans_msg = yandex.translate_message(user_message, lang_code, unique_lang_code)
        trans_text = trans_msg['text']
        trans_str = ''.join(trans_text)
        trans_msgs_dict[unique_lang_code] = trans_str

        if add_msg:
            msg_lang = add_trans_msg(unique_lang_id, trans_msg, message_id)

    return trans_msgs_dict 
Ejemplo n.º 6
0
    def test_caching_translation(self):
        """Test caching translation"""

        existing_message = yandex.translate_message("salut", "fr", "en")
        self.assertEqual(existing_message["text"], u"hi")
        self.assertEqual(existing_message["code"], 255)
Ejemplo n.º 7
0
    def test_yandex_translation(self):
        """Test yandex translation"""

        message = yandex.translate_message("Hello my friend", "en", "fr")
        self.assertEqual(message["text"], [u"Bonjour, mon ami"])
        self.assertEqual(message["code"], 200)
    def test_caching_translation(self):
        """Test caching translation"""

        existing_message = yandex.translate_message('salut', 'fr', 'en')
        self.assertEqual(existing_message["text"], u"hi")
        self.assertEqual(existing_message["code"], 255)
    def test_yandex_translation(self):
        """Test yandex translation"""

        message = yandex.translate_message('Hello my friend', 'en', 'fr')
        self.assertEqual(message["text"], [u"Bonjour, mon ami"])
        self.assertEqual(message["code"], 200)