How it Works¶
The limepkg have several functions to help you save a TRAML sendout. You only need to add some code where your normal traml sendout are handled in Python.
Basics¶
The creation of the traml cards is done from a class named TramlCardCreator:
from limepkg_traml_tracking.sdk import TramlCardCreator
TramlCardCreator(app=app, uow=uow)
TramlCardCreator(app=app)
Warning
Note the difference in the UOW if you pass that along to TramlCardCreator or not
- If you create TramlCardCreator class with a UOW, TramlCardCreator will not commit that UOW, you are responsible for do a
uow.commit()
call afterwards. (TramlCardCreator will however add everything to the UOW). - If you create TramlCardCreator class without the UOW, TramlCardCreator will create a UOW for everything and it will commit it for you.
Functions that are available for you in the TramlCardCreator class:
Save Email functions:¶
- save_traml_email_sendout - The normal add method from a send_result and Email object.
- save_traml_email_sendout_error - Save a email with status error from a Email object.
- save_traml_email_manually - For manually adding a record if you don’t want to use the two others.
Save SMS functions:¶
- save_traml_sms_sendout - The normal add method from a send_result and SMS object.
- save_traml_sms_sendout_error - Save a SMS with status error from a SMS object.
- save_traml_sms_manually - For manually adding a record if you don’t want to use the two others.
Save TRAML Email¶
You can use 3 functions in TramlCardCreator: save_traml_email_sendout, save_traml_email_sendout_error and save_traml_email_manually. We will now see how the 2 first works.
Let’s say you have this code already for your sendout in a custom limeobject:
email = traml.models.Email(
recipient_email=self.properties.email.value,
recipient_name=self.properties.name.value,
from_name="Lime Technologies",
from_email="[email protected]",
subject="My subject",
merge_codes={'{{firstname}}': self.properties.firstname.value}
)
tc = traml.tramlclient.TramlClient(app=self.application)
send_result = tc.send_transactionmail_by_template_name(
email, 'my-template'
)
tcc = TramlCardCreator(app=self.application, uow=uow)
tcc.save_traml_email_sendout(
send_result=send_result,
email=email,
relations={'person': self},
template_name="my-template",
)
from limepkg_traml_tracking.sdk import TramlCardCreator
Its a good practice to maybe have some error handling as well to be able to save a card with the status "Error". This is example code to also treat any error and still save a TRAML card (with the status "Error"):
try:
email = traml.models.Email(
recipient_email=self.properties.email.value,
recipient_name=self.properties.name.value,
from_name="Lime Technologies",
from_email="[email protected]",
subject="My subject",
merge_codes={'{{firstname}}': self.properties.firstname.value}
)
tc = traml.tramlclient.TramlClient(app=self.application)
send_result = tc.send_transactionmail_by_template_name(
email, 'my-template'
)
tcc = TramlCardCreator(app=self.application, uow=uow)
tcc.save_traml_email_sendout(
send_result=send_result,
email=email,
relations={'person': self},
template_name="my-template",
)
except Exception as e:
try:
tcc = TramlCardCreator(app=self.application, uow=uow)
tcc.save_traml_email_sendout_error(
email=email,
error_text=str(e),
relations={'person': self},
template_name="my-template"
)
except Exception as e:
logger.exception(f"Can’t save failed traml email: {e}")
Save TRAML SMS¶
You can use 3 functions in TramlCardCreator: save_traml_sms_sendout, save_traml_sms_sendout_error and save_traml_sms_manually. We will now see how the 2 first works.
Let’s say you have this code already for your sendout in a custom limeobject:
sms = traml.models.Sms(
'+4612345', self.properties.mobilephone.value
)
tc = traml.tramlclient.TramlClient(app=self.application)
send_result = tc.send_sms('My sms text', sms)
tcc = TramlCardCreator(app=self.application, uow=uow)
tcc.save_traml_sms_sendout(
send_result=send_result,
sms=sms,
text='My sms text',
relations={'person': self}
)
from limepkg_traml_tracking.sdk import TramlCardCreator
Its a good practice to maybe have some error handling as well to be able to save a card with the status "Error". This is example code to also treat any error and still save a TRAML card (with the status "Error"):
try:
sms = traml.models.Sms(
'+4612345', self.properties.mobilephone.value
)
tc = traml.tramlclient.TramlClient(app=self.application)
send_result = tc.send_sms('My sms text', sms)
tcc = TramlCardCreator(app=self.application, uow=uow)
tcc.save_traml_sms_sendout(
send_result=send_result,
sms=sms,
text='My sms text',
relations={'person': self}
)
except Exception as e:
try:
tcc = TramlCardCreator(app=self.application, uow=uow)
tcc.save_traml_sms_sendout_error(
sms=sms,
error_text=str(e),
relations={'person': self}
)
except Exception as e:
logger.exception(f"Can’t save failed traml sms: {e}")