Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -108,3 +108,83 @@ def corsproxy_post():
|
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
import os
|
| 115 |
+
import time
|
| 116 |
+
import requests
|
| 117 |
+
from datetime import datetime, timezone
|
| 118 |
+
|
| 119 |
+
URL = "https://desk-api.channel.io/desk/channels/200605/groups/519217/messages"
|
| 120 |
+
PARAMS = {
|
| 121 |
+
"sortOrder": "desc",
|
| 122 |
+
"limit": 36,
|
| 123 |
+
"logFolded": "false",
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
X_ACCOUNT = os.getenv("channeliotokenbot2")
|
| 127 |
+
if not X_ACCOUNT:
|
| 128 |
+
raise RuntimeError("環境変数 channeliotokenbot2 が設定されていません")
|
| 129 |
+
|
| 130 |
+
HEADERS = {
|
| 131 |
+
"accept": "application/json",
|
| 132 |
+
"accept-language": "ja",
|
| 133 |
+
"x-account": X_ACCOUNT,
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
def parse_updated_at(value):
|
| 137 |
+
"""
|
| 138 |
+
updatedAt が
|
| 139 |
+
- int / float : UNIX time (ms)
|
| 140 |
+
- str : ISO8601
|
| 141 |
+
のどちらでも処理できるようにする
|
| 142 |
+
"""
|
| 143 |
+
if isinstance(value, (int, float)):
|
| 144 |
+
# ミリ秒 → 秒
|
| 145 |
+
return datetime.fromtimestamp(value / 1000, tz=timezone.utc)
|
| 146 |
+
elif isinstance(value, str):
|
| 147 |
+
return datetime.fromisoformat(value.replace("Z", "+00:00"))
|
| 148 |
+
else:
|
| 149 |
+
return None
|
| 150 |
+
|
| 151 |
+
while True:
|
| 152 |
+
try:
|
| 153 |
+
response = requests.get(
|
| 154 |
+
URL,
|
| 155 |
+
headers=HEADERS,
|
| 156 |
+
params=PARAMS,
|
| 157 |
+
timeout=30,
|
| 158 |
+
)
|
| 159 |
+
response.raise_for_status()
|
| 160 |
+
|
| 161 |
+
data = response.json()
|
| 162 |
+
messages = data.get("messages", [])
|
| 163 |
+
|
| 164 |
+
latest_msg = None
|
| 165 |
+
latest_time = None
|
| 166 |
+
|
| 167 |
+
for msg in messages:
|
| 168 |
+
plain_text = msg.get("plainText")
|
| 169 |
+
updated_at = msg.get("updatedAt")
|
| 170 |
+
|
| 171 |
+
if not plain_text or updated_at is None:
|
| 172 |
+
continue
|
| 173 |
+
|
| 174 |
+
updated_time = parse_updated_at(updated_at)
|
| 175 |
+
if updated_time is None:
|
| 176 |
+
continue
|
| 177 |
+
|
| 178 |
+
if latest_time is None or updated_time > latest_time:
|
| 179 |
+
latest_time = updated_time
|
| 180 |
+
latest_msg = msg
|
| 181 |
+
|
| 182 |
+
if latest_msg:
|
| 183 |
+
print(latest_msg["plainText"])
|
| 184 |
+
else:
|
| 185 |
+
print("条件に合う message が見つかりませんでした")
|
| 186 |
+
|
| 187 |
+
except Exception as e:
|
| 188 |
+
print("エラー:", e)
|
| 189 |
+
|
| 190 |
+
time.sleep(60)
|