Exemplo n.º 1
0
    def check_contacts(self, contacts):
        """ Check if openmrs contact is saved in rapidpro. Returns a collection of rapidpro contacts"""
        contact_list = []
        last_checked = ConnectorUtils().get_last_checked(self.type_id)
        for contact in contacts:
            urn = util.urns_parser(contact.number)
            try:
                temba_contact = self.client.get_contacts(urn=urn).first()
                if temba_contact:
                    #If contact is found
                    contact_list.append(temba_contact)
                    if last_checked < contact.date_created:
                        last_checked = contact.date_created
                else:
                    #contact not found, save to rapidpro first
                    try:
                        contact_obj = self.client.create_contact(
                            name=contact.name, urns=[urn])
                        contact_list.append(contact_obj)

                        if last_checked < contact.date_created:
                            last_checked = contact.date_created
                    except TembaException as ex:
                        contact_list.append(None)
                        print ex
            except TembaException as ex:
                contact_list.append(None)
                print ex
        ConnectorUtils().update_last_checked(last_checked, self.type_id)
        return contact_list
Exemplo n.º 2
0
    def get_openmrs_contacts(self):
        """ Get contacts from OPENMRS """
        mrs_database = Database().get_openmrs_db()
        connection = vm.get_db_connector(mrs_database.hostname,
                                         mrs_database.username,
                                         mrs_database.password,
                                         mrs_database.database)
        last_checked = ConnectorUtils().get_last_checked(self.type_id)
        if self.type_id == ENROLLMENT_TYPE_ID:
            last_checked = ConnectorUtils().get_last_checked(self.type_id)
            contacts = vm.get_clients_enrollment_contacts(
                connection, last_checked)
        elif self.type_id == KICKOFF_TYPE_ID:
            last_checked = ConnectorUtils().get_last_checked(self.type_id)
            contacts = vm.get_kickoff_client_contacts(connection, last_checked)
        elif self.type_id == BIRTHDAY_TYPE_ID:
            contacts = vm.get_birthday_contacts(connection)
        elif self.type_id == APPOINTMENT_REMINDER_TYPE_ID:
            contacts = vm.get_appointment_booking_contacts(
                connection, last_checked)
        else:
            last_checked = ConnectorUtils().get_last_checked(self.type_id)
            contacts = vm.get_clients_enrollment_contacts(
                connection, last_checked)

        return contacts
Exemplo n.º 3
0
    def get_contacts(self):
        """ Get contacts from OpenMRS and send to RapidPro"""
        contact_list = []
        contacts = self.get_openmrs_contacts()
        last_checked = ConnectorUtils().get_last_checked(self.type_id)
        for contact in contacts:
            urns = [util.urns_parser(contact.number)]
            try:
                contact_obj = self.client.create_contact(name=contact.name,
                                                         urns=urns)
                contact_list.append(contact_obj)

                if last_checked < contact.date_created:
                    last_checked = contact.date_created
            except TembaException as ex:
                print ex

        ConnectorUtils().update_last_checked(last_checked, self.type_id)
        return contact_list
# Main connector file
import time
import schedule
from constants import KICKOFF_TYPE_ID, ENROLLMENT_TYPE_ID, APPOINTMENT_REMINDER_TYPE_ID
from SendMessage import SendMessage
from ConnectorUtils import ConnectorUtils


if __name__ == "__main__":
    #SendMessage(ENROLLMENT_TYPE_ID).broadcast_message()
    schedule.every(5).minutes.do(SendMessage(ENROLLMENT_TYPE_ID).broadcast_message)
    schedule.every(5).minutes.do(SendMessage(KICKOFF_TYPE_ID).broadcast_message)
    schedule.every(5).minutes.do(SendMessage(APPOINTMENT_REMINDER_TYPE_ID).broadcast_message)
    while True:
        schedule.run_pending()
        time.sleep(1)

    #create_contact()
    print ConnectorUtils().get_last_checked(ENROLLMENT_TYPE_ID)
    #SendMessage(ENROLLMENT_TYPE_ID).broadcast_message()
    #SendMessage(KICKOFF_TYPE_ID).broadcast_message()