This commit is contained in:
stupidcomputer 2024-11-10 22:42:37 -06:00
parent ea5d197833
commit 779cc2ccba
4 changed files with 62 additions and 66 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
tfbbridge.sqlite3
__pycache__/
*.pyc
tfbbridge/config.json

View File

@ -1,70 +1,26 @@
from flask import Flask, request, render_template, g, redirect
import requests
from .dbutils import get_and_init_db, get_db
from .groupme import group_sharing_url_to_id
from .groupme import group_sharing_url_to_id, get_oauth_auth_url, custom_get_oauth_auth
import json
import uuid
app = Flask(__name__)
app.config.from_mapping(
DATABASE_LOCATION = "tfbbridge.sqlite3",
BOT_ID = "4b841d36f2a0788192b5375335"
)
@app.route("/")
def testing():
return "testing"
@app.route("/add_bot", methods=["GET", "POST"])
def add_bot():
if request.method == "GET":
return render_template("add_new.html")
elif request.method == "POST":
try:
url = request.form["chaturl"]
try:
group_id = group_sharing_url_to_id(url)
except:
return "Bad URL", 400
db = get_db()
try:
db.execute(
"INSERT INTO child_groups (group_id) VALUES (?)",
(group_id,),
)
db.commit()
except db.IntegrityError:
return "This group is already registered."
bot_id = app.config["BOT_ID"]
print(bot_id)
requests.post("https://api.groupme.com/v3/bots?token={}".format(bot_id))
return url
except KeyError:
return "Bad URL", 400
else:
return "Invalid Method", 400
@app.route("/list_child_groups")
def list_child_groups():
db = get_db()
cursor = db.cursor()
cursor.execute('SELECT group_id FROM child_groups')
results = cursor.fetchall()
groups = [i[0] for i in results]
return groups
app.config.from_file("config.json", load=json.load)
@app.route("/groupme/oauth_callback")
def oauth_callback():
token = request.args.get("access_token")
print(token)
r = requests.get("https://api.groupme.com/v3/groups?token={}".format(token))
try:
print(r.text)
print(token)
data = r.json()["response"]
except KeyError:
return "Couldn't find something", 500
data = [
{
"group_id": i["group_id"],
@ -78,9 +34,6 @@ def oauth_callback():
def add_group():
token = request.form["token"]
target_group = request.form["to_add"]
print(target_group)
print(token, target_group)
headers = {
'Content-Type': 'application/json',
@ -100,15 +53,22 @@ def add_group():
r = requests.post('https://api.groupme.com/v3/bots', params=params, headers=headers, json=json_data)
payload = r.json()
bot_id = payload["response"]["bot_id"]
return payload
bot_id = payload["response"]["bot"]["bot_id"]
group_name = payload["response"]["bot"]["group_name"]
group_id = payload["response"]["bot"]["group_id"]
db = get_db()
try:
db.execute(
"INSERT INTO channels (group_name, group_id, bot_id, chan_type, organization)"
)
return r.text
@app.route("/groupme/authorize")
def authorize():
return redirect(
"https://oauth.groupme.com/oauth/authorize?client_id=SgAPMhA6H1UuBE2QD2O5JE1BM6WnysRu6AP1ib6jQsqrH3QA",
)
return redirect(get_oauth_auth_url())
@app.route("/groupme/create_organization", methods=["GET", "POST"])
def create_organization():
@ -165,5 +125,5 @@ def add_chan_to_org(org):
cursor.execute('SELECT id, org_name FROM organizations WHERE addition_url=?', (org,))
org_id, name = cursor.fetchone()
return redirect(
"https://oauth.groupme.com/oauth/authorize?client_id=SgAPMhA6H1UuBE2QD2O5JE1BM6WnysRu6AP1ib6jQsqrH3QA&state=" + org,
"https://oauth.groupme.com/oauth/authorize?client_id=qAAAc8GSUlTA8C3Ypo9CMiFQwQCQnU8zPU5KGEtz3FYHDqP5&response_type=token"
)

View File

@ -1,6 +1,9 @@
# misc groupme utilities
from urllib.parse import urlparse
from urllib.parse import urlparse, urlencode
from flask import current_app
from typing import Optional
def group_sharing_url_to_id(url: str) -> str:
"""
@ -17,3 +20,35 @@ def group_sharing_url_to_id(url: str) -> str:
group_id = splitted[2]
return group_id
def build_oauth_auth_url(client_id: str, state: Optional[str] = None, user_params: Optional[dict] = {}):
"""
Build the OAuth redirect url to GroupMe.
state is optional -- it is usually in reference to the organization in question.
If so, state should be set to the organization's `addition_url`. (see schema.sql for info)
"""
base = "https://oauth.groupme.com/oauth/authorize?"
params = {}
params["client_id"] = client_id
if state:
params["state"] = state
params = params | user_params
print(base + urlencode(params))
print(params)
return base + urlencode(params)
def get_oauth_auth_url(state: None | str = None):
return build_oauth_auth_url(
current_app.config["CLIENT_ID"],
state
)
def custom_get_oauth_auth(params):
return build_oauth_auth_url(
current_app.config["CLIENT_ID"],
None,
params
)

View File

@ -4,8 +4,8 @@ DROP TABLE IF EXISTS parent_groups;
CREATE TABLE organizations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
org_name TEXT NOT NULL,
admin_url TEXT UNIQUE NOT NULL,
addition_url TEXT UNIQUE NOT NULL
admin_url TEXT UNIQUE NOT NULL, /* should be private */
addition_url TEXT UNIQUE NOT NULL /* can be public */
);
CREATE TABLE channels (