Davkodi Cyber

Python Automatic Messenger Using the GroupMe API

In this post page I will walkthrough a little side project I did to automatically send messages through GroupMe using a Python library called groupy, which takes advantage of GroupMe's API, or Application Programming Interface.

Groupy Library Documentation HERE

Daily Quote Sender

In this progrom I use the groupy library to create a client which can list groups, chat, bots, etc. It can also be used to post messages. In order for this to work you will need to grab your token from the GroupMe Developers website. You will need to log in to your account HERE. The token button will be in the top right corner.

In this project I collected hundreds of quotes from a json file which I downloaded to my PI. Using the libary I was able to put all the groups that my account is associated with into the list to find the Daily Quotes group. Thankfully, Json is super easy to extract specific data from so I was able to pick a random number within the length of a file and pull the quote and author. Then, using the post function, I can send the message from my Raspberry Pi, which is acting as the client.

I needed this program to run every day at the same time so I made use of the linux crontab, which is essentially a scheduler. To get my program to run everyday at 8 A.M. I used this line in the crontab. To access the crontab type in crontab -e in your linux terminal.

                0 8 * * 3 python3 dailyQuoteSender.py quotes.json
                
From left to right each field denotes the: Minute, Hour, Day of the Month, Month, Day of the Week, and finally the command you want to run.

            from groupy.client import Client
            import json
            import random
            import sys

            f = open(sys.argv[1])
            data = json.load(f)
            randNum = random.randint(0, len(data))

            token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            client = Client.from_token(token)
            group = ""

            groups = list(client.groups.list_all())
            for g in groups:
                if g.name == "Daily Quote":
                    group = g

            message = "Daily Quote: \n\n" + data[randNum]['quote'] + " \n\t-" + data[randNum]['author'] + "\n"

            print(message)
            message = group.post(text=message)

            f.close()