CORVO-AI commited on
Commit
ad710e4
·
verified ·
1 Parent(s): 275f066

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -113
app.py CHANGED
@@ -1,116 +1,39 @@
1
- from flask import Flask, request, jsonify, render_template_string
2
- from datetime import datetime
3
 
4
  app = Flask(__name__)
5
 
6
- # In-memory "DB"
7
- USERS = [] # list of dicts: {id, first_name, username, created_at, last_seen}
8
- USERS_BY_ID = {} # fast lookup by telegram user id
9
-
10
-
11
- def upsert_user(user_id: int, first_name: str, username: str | None):
12
- """
13
- Insert user if new, otherwise update last_seen + latest name/username.
14
- Returns: (is_new: bool, user_dict: dict)
15
- """
16
- now = datetime.utcnow().isoformat(timespec="seconds") + "Z"
17
- username = (username or "").strip()
18
- first_name = (first_name or "").strip()
19
-
20
- if user_id in USERS_BY_ID:
21
- u = USERS_BY_ID[user_id]
22
- # update fields (keep it fresh)
23
- u["first_name"] = first_name
24
- u["username"] = username
25
- u["last_seen"] = now
26
- return False, u
27
-
28
- u = {
29
- "id": int(user_id),
30
- "first_name": first_name,
31
- "username": username, # may be empty
32
- "created_at": now,
33
- "last_seen": now,
34
- }
35
- USERS.append(u)
36
- USERS_BY_ID[user_id] = u
37
- return True, u
38
-
39
-
40
- @app.get("/api/users")
41
- def get_users():
42
- # returns all users
43
- return jsonify({
44
- "count": len(USERS),
45
- "users": USERS
46
- })
47
-
48
-
49
- @app.get("/api/users/upsert")
50
- def upsert_user_get():
51
- """
52
- Example:
53
- /api/users/upsert?id=123&first_name=Omar&username=omar123
54
- """
55
- user_id = request.args.get("id", type=int)
56
- first_name = request.args.get("first_name", default="", type=str)
57
- username = request.args.get("username", default="", type=str)
58
-
59
- if not user_id:
60
- return jsonify({"ok": False, "error": "Missing required param: id"}), 400
61
-
62
- is_new, u = upsert_user(user_id, first_name, username)
63
- return jsonify({"ok": True, "is_new": is_new, "user": u})
64
-
65
-
66
- @app.get("/show")
67
- def show_users():
68
- # Simple HTML table
69
- html = """
70
- <!doctype html>
71
- <html>
72
- <head>
73
- <meta charset="utf-8" />
74
- <title>Users</title>
75
- <style>
76
- body { font-family: Arial, sans-serif; padding: 20px; }
77
- table { border-collapse: collapse; width: 100%; }
78
- th, td { border: 1px solid #ddd; padding: 10px; text-align: left; }
79
- th { background: #f5f5f5; }
80
- .muted { color: #777; }
81
- </style>
82
- </head>
83
- <body>
84
- <h2>Saved Users ({{count}})</h2>
85
- <p class="muted">In-memory only (will reset if server restarts).</p>
86
- <table>
87
- <thead>
88
- <tr>
89
- <th>ID</th>
90
- <th>First name</th>
91
- <th>Username</th>
92
- <th>Created</th>
93
- <th>Last seen</th>
94
- </tr>
95
- </thead>
96
- <tbody>
97
- {% for u in users %}
98
- <tr>
99
- <td>{{u.id}}</td>
100
- <td>{{u.first_name}}</td>
101
- <td>{{('@' + u.username) if u.username else ''}}</td>
102
- <td>{{u.created_at}}</td>
103
- <td>{{u.last_seen}}</td>
104
- </tr>
105
- {% endfor %}
106
- </tbody>
107
- </table>
108
- </body>
109
- </html>
110
- """
111
- return render_template_string(html, users=USERS, count=len(USERS))
112
-
113
-
114
- if __name__ == "__main__":
115
- # Run: python server.py
116
- app.run(host="0.0.0.0", port=7860, debug=True)
 
1
+ from flask import Flask, request, redirect, url_for
 
2
 
3
  app = Flask(__name__)
4
 
5
+ # In-memory storage for chat messages
6
+ messages = []
7
+
8
+ # --- Home page ---
9
+ @app.route('/', methods=['GET', 'POST'])
10
+ def index():
11
+ global messages
12
+ html = "<html><body>"
13
+ html += "<h2>Simple Chat Share</h2>"
14
+
15
+ if request.method == 'POST':
16
+ text = request.form.get('text')
17
+ if text:
18
+ messages.append(text)
19
+ return redirect(url_for('index'))
20
+
21
+ # Chat input form
22
+ html += "<form method='POST'>"
23
+ html += "Message:<br>"
24
+ html += "<input type='text' name='text' style='width:300px'>"
25
+ html += "<input type='submit' value='Send'>"
26
+ html += "</form><hr>"
27
+
28
+ # Display messages with copy button
29
+ html += "<h3>Messages:</h3>"
30
+ for i, msg in enumerate(messages[::-1], 1): # newest first
31
+ # Using simple input box to allow easy copy/paste
32
+ html += f"<p>{i}: <input type='text' value='{msg}' readonly style='width:300px'> <button onclick='this.previousElementSibling.select();document.execCommand(\"copy\");'>Copy</button></p>"
33
+
34
+ html += "</body></html>"
35
+ return html
36
+
37
+ # --- Run the app ---
38
+ if __name__ == '__main__':
39
+ app.run(host='0.0.0.0', port=7806)