-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
113 lines (83 loc) · 2.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# [START gae_python39_app]
import logging
import os
import requests
import json
from flask import Flask, request, redirect
from telegram import Update
import tweepy
from models.phrase import LongPhrase
from slack.handlers import handle_slack
from tg import tg_dispatcher
from tg.handlers import handle_ping as handle_telegram_ping
# Enable logging
logging.basicConfig(format="%(message)s", level=logging.INFO)
TG_TOKEN = os.environ["TG_TOKEN"]
BASE_URL = os.environ["BASE_URL"]
PORT = os.environ.get("PORT", 5050)
app = Flask(__name__)
@app.route("/")
def ping():
return "I am alive"
@app.route(f"/{TG_TOKEN}", methods=["POST"])
def telegram_handler():
dispatcher = tg_dispatcher()
update = Update.de_json(request.json, dispatcher.bot)
dispatcher.process_update(update)
return "Handled"
@app.route(f"/{TG_TOKEN}/ping", methods=["GET"])
def telegram_ping_handler():
dispatcher = tg_dispatcher()
handle_telegram_ping(dispatcher.bot)
return "OK"
@app.route("/slack", methods=["POST"])
def slack_handler():
data = request.form
if "payload" in data:
data = json.loads(data["payload"])
response = handle_slack(data)
if response:
response_url = data["response_url"]
requests.post(response_url, json=response["indirect"])
return response["direct"]
return ""
@app.route("/slack/auth", methods=["GET"])
def slack_auth_handler():
client_id = os.environ["SLACK_CLIENT_ID"]
scopes = ["commands", "chat:write", "chat:write.public"]
return redirect(
f'https://slack.com/oauth/v2/authorize?client_id={client_id}&scope={",".join(scopes)}'
)
@app.route("/slack/auth/redirect", methods=["GET"])
def slack_auth_redirect_handler():
code = request.args.get("code")
request_body = {
"code": code,
"client_id": os.environ["SLACK_CLIENT_ID"],
"client_secret": os.environ["SLACK_CLIENT_SECRET"],
}
requests.post(
"https://slack.com/api/oauth.v2.access",
request_body,
) # We dont want the token
return ":)"
@app.route("/twitter/auth/redirect", methods=["GET"])
def twitter_auth_redirect_handler():
return ":)"
@app.route("/twitter/ping", methods=["GET"])
def twitter_ping_handler():
client = tweepy.Client(
consumer_key=os.environ["TWITTER_CONSUMER_KEY"],
consumer_secret=os.environ["TWITTER_CONSUMER_KEY_SECRET"],
access_token=os.environ["TWITTER_ACCESS_TOKEN"],
access_token_secret=os.environ["TWITTER_ACCESS_TOKEN_SECRET"],
)
client.create_tweet(text=LongPhrase.get_random_phrase().text)
return ""
if __name__ == "__main__":
print(TG_TOKEN)
# This is used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
app.run(host="0.0.0.0", port=PORT, debug=True)
# [END gae_python39_app]