Skip to main content

A simple bot builder for GroupMe

Project description

GroupMe Bot Builder

Easily build one or more bots into a single application. Supports regex handlers of incoming messages as well as cron jobs to perform functions on a regular cadence.

pip install groupme-bot

Usage

  • An Application object is created to house one or many Bot objects and route incoming traffic to the correct Bot. Each Bot is registered at it's own url path to allow for Bots to easily setup callbacks in the GroupMe Developer site.
  • A separate Bot object is defined for each bot and can include the following handlers:
    • Regexp Handlers: If a message is sent to the group that matches the given regex, the associated handler function will be called
    • Cron Jobs: Handler functions that will be run on a set cron cadence
  • Handler functions all take one argument (context) which is of type Context. The Context contains both a reference to the Bot object being called and the Callback object containing the payload from GroupMe.
    • The passing of the Bot object in the Context allows for handler functions to be universal and shared by multiple Bots.

Running Your App

Start your app with Uvicorn. For more deployment details, see http://www.uvicorn.org/deployment/.

Example running an app object in main.py.

Must use --workers=1 to prevents scheduled jobs from running multiple times. Working on a fix for this.

uvicorn main:app --workers=1

Multi Bot Example

# main.py

import re 

from groupme_bot import Application, Bot, Context, ImageAttachment, LocationAttachment


# create the bot Application
app = Application()


# define handler functions
def cron_task(ctx: Context):
    print(ctx.bot.bot_name)
    print("this is a scheduled function at the top of every hour")

def mention_all(ctx: Context):
    ctx.bot.mention_all()

def attachments(ctx: Context):
    img_url = ctx.bot.image_url_to_groupme_image_url(image_url="https://images.indianexpress.com/2020/12/Doodle.jpg")
    image_attachment = ImageAttachment(image_url=img_url)
    location_attachment = LocationAttachment(name="A Location", lat=100.000, lng=46.000)
    ctx.bot.post_message("this is a message with attachments", [image_attachment, location_attachment])

def gif_search(ctx: Context):
    sr = re.search(r'^\\gif([a-zA-Z0-9 -_]+)', ctx.callback.text)
    if sr:
        query_string = sr.group(1).strip()
        # gif_result = search_for_gif(query_string)
        # ctx.bot.post_message(gif_result)
        print("implement something like this ^")


# build the bot objects
bot1 = Bot('Fake bot 1',
           bot_id='fake-bot-id',
           groupme_api_token='fake-token',
           group_id='fake-group-id')

bot2 = Bot('Fake bot 2',
           bot_id='fake-bot-id',
           groupme_api_token='fake-token',
           group_id='fake-group-id')

# add cron job
#  - available cron_task arguments: https://apscheduler.readthedocs.io/en/stable/modules/triggers/cron.html
bot1.add_cron_job(cron_task, minute=0, hour='*', timezone='America/Chicago')

# add callback handlers
bot1.add_callback_handler(r'^\\attachments', attachments)  # message starts with the string '\attachments'
bot1.add_callback_handler(r'^\\all', mention_all)  # message starts with the string '\all'
bot2.add_callback_handler(r'^\\all', mention_all)  # message starts with the string '\all'
bot2.add_callback_handler(r'^\\gif', gif_search)  # message starts with the string '\gif'


# add the bots to the bot router
app.add_bot(bot=bot1, callback_path="/bot1")
app.add_bot(bot=bot2, callback_path="/bot2")

# to run:
# `uvicorn main:app --workers=1`

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

groupme-bot-0.2.3.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

groupme_bot-0.2.3-py2.py3-none-any.whl (11.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file groupme-bot-0.2.3.tar.gz.

File metadata

  • Download URL: groupme-bot-0.2.3.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.7.4

File hashes

Hashes for groupme-bot-0.2.3.tar.gz
Algorithm Hash digest
SHA256 b9755a2eb73e76ae03ae816d319b9362ae0194292dbed1de908e24f139c1f05e
MD5 1536662d2d41587c2f71bf537cf655a8
BLAKE2b-256 196ea2d7ce8c77863c49d7084fe42fa6965d7371995b27e31f657b555b0e4745

See more details on using hashes here.

File details

Details for the file groupme_bot-0.2.3-py2.py3-none-any.whl.

File metadata

  • Download URL: groupme_bot-0.2.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.7.4

File hashes

Hashes for groupme_bot-0.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7a0d330a285fd608dbc43de1ca945b0f2952b5af6dd9c350aa152d3160be5fa9
MD5 140b2a5ccc9a9bb9b91418df9c821786
BLAKE2b-256 455e020aba6e26c353402ccfc1512f997c9bc355d5cbe32630f78b35536308d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page