Skip to main content

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`

Metadata

Release files for groupme-bot 0.2.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for groupme-bot 0.2.13
File Size Uploaded
groupme_bot-0.2.13.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for groupme-bot 0.2.13
File Interpreter ABI Platform
groupme_bot-0.2.13-py3-none-any.whl Python 3 none any Details

Total release size: 23.9 kB

Release files / groupme_bot-0.2.13.tar.gz

Download URL groupme_bot-0.2.13.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3881caf05b1453431726315c44e8a83c23bb26464be76f1362671876f3100ef2
BLAKE2b-256 checksum
How to use checksums
615295ef576906259a9d28cb9d7e1dfd520ffd5a89078d701f3bab8d37fd41d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Darwin/22.1.0

Release files / groupme_bot-0.2.13-py3-none-any.whl

Download URL groupme_bot-0.2.13-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ce79908c27120ebd49b2af9982b6ed4eff997800ef46518593dd4ed38213d32
BLAKE2b-256 checksum
How to use checksums
0e9e0d531a49973cbd3d4355867ad66fa916dc8647357cd3efd85df620ae0dcb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Darwin/22.1.0

Release history Release notifications | RSS feed

This release

0.2.13 This release

2 release files

0.2.12

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

3 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

1 release file

0.1.4

2 release files

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page