Yesterday I found the following Twitter account and I thought that it was pretty funny so I decided to make my own implementation but instead of using Big Ben I decided to honor my great hometown, Skara!

I've worked a lot with Python lately and started to like it so I wrote the bot in Python as well. Python is a wonderful language and there're many fine libraries that simplifies the development a lot. So, the idea of the bot is simple, every hour it should report the time on Twitter, but we will not print out just the time, it's boring, so we will also print out our BONGS (the sound a church bell does). To make this happen, I wrote a python script that runs every hour(cronjob on Ubuntu) that checks how many times it should print out BONG and then posts it to Twitter. Time for the code:

from twitter import OAuth, Twitter
from datetime import datetime, time
import pytz

TOKEN = "SECRET"
TOKEN_KEY = "SECRET"
API_KEY = "SECRET"
API_SECRET = "SECRET"

MY_TZ = pytz.timezone('Europe/Stockholm')
now = datetime.now(MY_TZ).time()
def get_current_hour(timestamp):
    return timestamp.hour % 12

def compose_tweet(timestamp):
    number_of_rings = get_current_hour(timestamp)
    # Creates a tweet, e.g. "BONG BONG BONG #03:00
    alert_sound = "BONG"
    tweet = " ".join([alert_sound] * number_of_rings)
    hashtag = "#%s:%s" %(str(timestamp.hour).zfill(2), str(timestamp.minute).zfill(2))
    return "%s %s" %(tweet, hashtag)

def send_tweet(tweet):
    auth = OAuth(TOKEN, TOKEN_KEY, API_KEY, API_SECRET)
    t = Twitter(auth=auth)
    t.statuses.update(status=tweet)

def main(timestamp = now):
    send_tweet(compose_tweet(timestamp))

if __name__ == "__main__":
    main()