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')
Exemple #2
0
    def wod_is_posted(self, url):
        while not self.found_WOD:
            self.scrape_programming(url)
            date = self.get_current_dates()

            if date['tomorrow'] in self.parsed_list:
                self.config.read('config.ini')
                body = f"Found tomorrow's ({date['tomorrow']}) WOD at {url}"
                print(body)
                for r, value in list(self.config.items('to')):
                    print(value)
                    twilio_api.send_message(self.config['from']['pa'], value, body)
                self.found_WOD = True
                self.check_date_change(date['today'])
            else:
                print(f"{pendulum.time.now(false)}: {pendulum.time.now(False)}: Tomorrow's WOD ({date['tomorrow']}) has not been posted yet.")
                time.sleep(5)
def send_trans_texts(contacts, trans_msgs_dict, message_id):
    """ [], dict, int -> None
    Send translated texts to contacts"""
    user_messages = Message.query.count()
    if user_messages < 2000:
        for contact in contacts:
            contact_lang = contact.language.yandex_lang_code
            msg = twilio_api.send_message(trans_msgs_dict[contact_lang], contact.contact_phone)
            contact_msg = add_sent_msg(msg.status, contact.contact_id, message_id)