Groupme Bot Posting Twice

360 Views Asked by At

I made a GroupMe bot that is supposed to echo what a specific user says, but in ALL CAPS. I used this blog to set up the bot which runs on a python server using gunicorn and Flask which is then hosted with Heroku. I have generated the bot ID and the bot successfully echoes the user, but it does so twice. I used this short code to operate the bot:

import os
import json

from urllib.parse import urlencode
from urllib.request import Request, urlopen

from flask import Flask, request

app = Flask(__name__)
bot_id_var = 'TEST_BOT_ID'

# The url that we want to POST to
POST_url = 'https://api.groupme.com/v3/bots/post'

@app.route('/', methods=['POST'])
def webhook():
    json_data = request.get_json()

    if json_data['name'] == 'Dylan Powers':
        # Make the message all upper case
        message = json_data['text'].upper()
        send_message(message)

# Constructs the message that we would like to send
def send_message(message):

    POST_data = {
                # Get the bot ID from environment variables
                'bot_id': os.getenv(bot_id_var),
                'text': message
                }
    request = Request(POST_url, urlencode(POST_data).encode())
    json = urlopen(request).read().decode()

TEST_BOT_ID refers to an environment variable that I created that holds the bot's ID. Does anybody know why this is posting twice to group chats?

0

There are 0 best solutions below