<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def announcement_posted_hook(request, obj):
 """Runs whenever a new announcement is created, or a request is approved and posted. obj: The Announcement object """

    logger.debug("Announcement posted")

    if obj.notify_post:
        logger.debug("Announcement notify on")
        announcement_posted_twitter(request, obj)
        try:
            notify_all = obj.notify_email_all
        except AttributeError:
            notify_all = False

        try:
            if notify_all:
                announcement_posted_email(request, obj, True)
            else:
                announcement_posted_email(request, obj)
        except Exception as e:
            logger.error("Exception when emailing announcement: {}".format(e))
            messages.error(request, "Exception when emailing announcement: {}".format(e))
            raise e
    else:
        logger.debug("Announcement notify off")