Spaces:
Running
Running
Create watch.py
Browse files
watch.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
from datetime import datetime, timezone
|
| 5 |
+
|
| 6 |
+
URL = "https://desk-api.channel.io/desk/channels/200605/groups/519217/messages"
|
| 7 |
+
PARAMS = {
|
| 8 |
+
"sortOrder": "desc",
|
| 9 |
+
"limit": 36,
|
| 10 |
+
"logFolded": "false",
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
X_ACCOUNT = os.getenv("channeliotokenbot2")
|
| 14 |
+
if not X_ACCOUNT:
|
| 15 |
+
raise RuntimeError("環境変数 channeliotokenbot2 が設定されていません")
|
| 16 |
+
|
| 17 |
+
HEADERS = {
|
| 18 |
+
"accept": "application/json",
|
| 19 |
+
"accept-language": "ja",
|
| 20 |
+
"x-account": X_ACCOUNT,
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
def parse_updated_at(value):
|
| 24 |
+
if isinstance(value, (int, float)):
|
| 25 |
+
return datetime.fromtimestamp(value / 1000, tz=timezone.utc)
|
| 26 |
+
elif isinstance(value, str):
|
| 27 |
+
return datetime.fromisoformat(value.replace("Z", "+00:00"))
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
def main():
|
| 31 |
+
while True:
|
| 32 |
+
try:
|
| 33 |
+
response = requests.get(
|
| 34 |
+
URL,
|
| 35 |
+
headers=HEADERS,
|
| 36 |
+
params=PARAMS,
|
| 37 |
+
timeout=30,
|
| 38 |
+
)
|
| 39 |
+
response.raise_for_status()
|
| 40 |
+
|
| 41 |
+
data = response.json()
|
| 42 |
+
messages = data.get("messages", [])
|
| 43 |
+
|
| 44 |
+
latest_msg = None
|
| 45 |
+
latest_time = None
|
| 46 |
+
|
| 47 |
+
for msg in messages:
|
| 48 |
+
plain_text = msg.get("plainText")
|
| 49 |
+
updated_at = msg.get("updatedAt")
|
| 50 |
+
|
| 51 |
+
if not plain_text or updated_at is None:
|
| 52 |
+
continue
|
| 53 |
+
|
| 54 |
+
updated_time = parse_updated_at(updated_at)
|
| 55 |
+
if not updated_time:
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
+
if latest_time is None or updated_time > latest_time:
|
| 59 |
+
latest_time = updated_time
|
| 60 |
+
latest_msg = msg
|
| 61 |
+
|
| 62 |
+
if latest_msg:
|
| 63 |
+
print(latest_msg["plainText"])
|
| 64 |
+
else:
|
| 65 |
+
print("条件に合う message が見つかりませんでした")
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print("エラー:", e)
|
| 69 |
+
|
| 70 |
+
time.sleep(60)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
main()
|