This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
138 lines (109 loc) · 4.29 KB
/
bot.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import html, json, re, requests, traceback
from flask import Flask, request
from flask_cors import CORS
app = Flask(__name__)
from chatbot import *
with open("../configurations/vybot.txt", "r") as f:
secret = f.read().strip()
hooks = {}
def handler(room):
def _inner(activity):
if room != rid:
return
if "e" in activity:
for x in activity["e"]:
print(x)
if (
x["user_id"] == 281362
and x.get("content") == "!!/swap-rooms"
):
send("I am now leaving this room. Goodbye!")
swap()
send("Hello! I am now operating in this room.")
if x["event_type"] == 1 and x["room_id"] == room:
if x["user_id"] == 296403:
continue
try:
r = requests.post(
"http://localhost:5666/msg",
headers={"Content-Type": "application/json"},
json={"secret": secret, "message": x},
)
if r.status_code == 200 and r.text:
hooks[x["message_id"]] = send(r.text)
except:
traceback.print_exc()
if x["event_type"] == 2 and x["room_id"] == room:
if x["user_id"] == 296403:
continue
try:
r = requests.post(
"http://localhost:5666/msg",
headers={"Content-Type": "application/json"},
json={"secret": secret, "message": x},
)
if r.status_code == 200 and r.text:
if x["message_id"] in hooks:
rooms[rid].editMessage(
r.text, hooks[x["message_id"]]
)
else:
hooks[x["message_id"]] = send(r.text)
elif x["message_id"] in hooks:
rooms[rid].editMessage(
"(message response failed or the new message is"
" not a command)",
hooks[x["message_id"]],
)
except:
traceback.print_exc()
if x["event_type"] == 3 and x["room_id"] == room:
if x["user_id"] == 296403:
continue
r = requests.post(
"http://localhost:5666/join",
headers={"Content-Type": "application/json"},
json={"secret": secret, "data": x},
)
if r.status_code == 200 and r.text:
send(r.text)
if x["event_type"] == 10 and x["room_id"] == room:
if x["user_id"] == 296403:
continue
if x["message_id"] in hooks:
rooms[rid].deleteMessage(hooks[x["message_id"]])
return _inner
chatbot = Chatbot()
chatbot.login()
rid = 106764
def swap():
global rid
rid = 106765 - rid
rooms = {k: chatbot.joinRoom(k, handler(k)) for k in [1, 106764]}
def send(message):
return rooms[rid].sendMessage(message)
@app.route("/", methods=["POST"])
def post_message():
data = request.json
msg = send(data["message"])
if data.get("pin"):
chatbot.sendRequest(
f"http://chat.stackexchange.com/messages/{msg}/owner-star",
"post",
{"fkey": chatbot.fkey},
headers={
"Referer": f"http://chat.stackexchange.com/rooms/{rid}",
"Origin": "http://chat.stackexchange.com",
},
)
return str(msg)
@app.route("/kill")
def kill():
request.environ.get("werkzeug.server.shutdown")()
@app.route("/edit", methods=["POST"])
def edit_message():
data = request.json
rooms[rid].editMessage(data["message"], data["id"])
return ""
if __name__ == "__main__":
app.run(host="localhost", port=5888)