How to handle log activity

I have created a custom app, and inside that I have created customerticket doctype, now what i want is when I hit api for add_comment if the user is not came as argument then comment_by = frappe.session.user else created_by = user and it should be appear in activity means if I am getting user as argument then in actvity section name of user and the comment which he add should be appear, is it possible if yes how to do that?
this is my function @frappe.whitelist(allow_guest=True)
def add_comment_with_created_by(doctype, docname, message, user=None):
doc = frappe.get_doc(doctype, docname)
comment = “Comment”
if user:
created_by = user
else:
created_by = frappe.session.user

doc.add_comment(comment_type=comment, comment_by=created_by, text=message )

doc.save()