2

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?

Dylan Powers
  • 209
  • 4
  • 10
  • Do you actually have 2 post requests? And isn't the webhook triggered even when the bot posts the message? – Adelin Mar 15 '18 at 05:57
  • 1
    I don't think it will be the case of Bot messages coming to webhook. In that case, it should just go in a loop? Can you log the payload, may be GroupMe is sending 2 different payloads? Like msg delivered etc. which you are not checking. Just a hunch! – Shamik Mar 15 '18 at 06:20
  • @Adelin I'm not sure, where would the second post request come from? – Dylan Powers Mar 15 '18 at 19:43

0 Answers0