kkk2z1 commited on
Commit
31f1786
·
verified ·
1 Parent(s): 6ff6b83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -34
app.py CHANGED
@@ -1,67 +1,185 @@
1
  import os
2
- import json
3
  import time
 
 
4
  import requests
5
- from datetime import datetime, timedelta
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- # ===== 設定 =====
8
- CHANNEL_ID = "200605" # チャンネルID
9
- GROUP_ID = "534997" # グループID
10
 
11
- MESSAGES_URL = (
12
- f"https://desk-api.channel.io/desk/channels/"
13
- f"{CHANNEL_ID}/groups/{GROUP_ID}/messages"
14
- )
15
 
16
- X_ACCOUNT = os.getenv("CHANNEL_IO_TOKEN")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  if not X_ACCOUNT:
18
- raise RuntimeError("環境変数 CHANNEL_IO_TOKEN が設定されていません")
19
 
20
  HEADERS = {
21
  "accept": "application/json",
 
22
  "content-type": "application/json",
23
  "x-account": X_ACCOUNT,
24
  }
25
 
26
- # ===== メッセージ送信関数 =====
27
- def send_message(text: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  try:
29
  payload = {
30
  "requestId": f"desk-web-{int(time.time() * 1000)}",
31
  "blocks": [
32
  {
33
- "type": "text",
34
  "value": text
35
  }
36
- ]
37
  }
38
 
39
  res = requests.post(
40
  MESSAGES_URL,
41
  headers=HEADERS,
42
  data=json.dumps(payload),
43
- timeout=30
44
  )
45
  res.raise_for_status()
46
- print("送信成功")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- except requests.RequestException as e:
49
- print(f"通信エラー: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- def wait_until_next_minute():
52
- now = datetime.now()
53
- next_minute = (now + timedelta(minutes=1)).replace(second=0, microsecond=0)
54
- time.sleep((next_minute - now).total_seconds())
55
 
56
- # ===== 実行 =====
57
  if __name__ == "__main__":
58
- #time_datetime= datetime.now()
59
- #print(f"okay!{time_datetime}")
60
- #send_message(f"起動なう: {time_datetime}")
61
- #send_message(f"botですが何か?")
62
- wait_until_next_minute()
63
- while True:
64
- time_datetime= datetime.now()
65
- send_message(f"now:{time_datetime}")
66
- print(f"送信しました:{time_datetime}")
67
- wait_until_next_minute()
 
1
  import os
 
2
  import time
3
+ from datetime import datetime
4
+ import json
5
  import requests
6
+ import traceback
7
+ import random
8
+
9
+
10
+
11
+ def log(msg,type):
12
+ if type == 1:#通常ログ
13
+ time_datetime= datetime.now()
14
+ print(f"{time_datetime}[log]:{msg}")
15
+ elif type == 2: #警告
16
+ time_datetime= datetime.now()
17
+ print(f"{time_datetime}[warn]:{msg}")
18
+ elif type == 3: #エラー
19
+ time_datetime= datetime.now()
20
+ print(f"{time_datetime}[error]:{msg}")
21
+ traceback.print_exc()
22
+
23
+
24
+ def System_up(time):
25
+ START_TIME = time
26
+ while START_TIME > 0:
27
+ log(f"起動まで、{START_TIME}秒",1)
28
+ START_TIME = START_TIME - 1
29
+ time.sleep(1)
30
+
31
 
 
 
 
32
 
33
+ System_up(3)
 
 
 
34
 
35
+
36
+ log("起動しました。",1)
37
+
38
+
39
+ # ===== Channel.io 設定 =====
40
+ CHANNEL_ID = os.getenv("CHIO_CH_ID")#"200605" チャンネル
41
+ if not CHANNEL_ID:
42
+ raise RuntimeError("環境変数 CHIO_CH_ID が設定されていません")
43
+
44
+ GROUP_ID = os.getenv("CHIO_GR_ID")#"534868" グループ
45
+ if not GROUP_ID:
46
+ raise RuntimeError("環境変数 CHIO_GR_ID が設定されていません")
47
+
48
+ MESSAGES_URL = f"https://desk-api.channel.io/desk/channels/{CHANNEL_ID}/groups/{GROUP_ID}/messages"
49
+
50
+ X_ACCOUNT = os.getenv("CHIO_TOKEN") #トークン
51
  if not X_ACCOUNT:
52
+ raise RuntimeError("環境変数 CHIO_TOKEN が設定されていません")
53
 
54
  HEADERS = {
55
  "accept": "application/json",
56
+ "accept-language": "ja",
57
  "content-type": "application/json",
58
  "x-account": X_ACCOUNT,
59
  }
60
 
61
+ PARAMS = {
62
+ "sortOrder": "desc",
63
+ "limit": 50,
64
+ }
65
+
66
+ # ===== Utils =====
67
+
68
+ def ramdom_msg():
69
+ date = [
70
+ {"no":0,"by":"name","text":"message","call":"addres"},#0
71
+ {"no":1,"by":"name","text":"message","call":"addres"},#0
72
+ {"no":2,"by":"name","text":"message","call":"addres"},#0
73
+
74
+ ]
75
+ rand = random.randint(0, 1)#ランダム最大値指定。
76
+ if rand == 0:
77
+ ramdom_msg()#再起
78
+ log("辞書の0が指定されたから無視",1)
79
+ elif rand == 1:
80
+ try:
81
+ text = date[1]["text"]
82
+ by = date[1]["by"]
83
+ call = date[1]["call"]
84
+ send_to_channel(f"広告:{text}")
85
+ send_to_channel(f"{by}様より。call:{call}")
86
+ except Exception as e:
87
+ log(f"広告{rand}番エラー:{e}",3)
88
+ elif rand == 2:
89
+ try:
90
+ text = date[2]["text"]
91
+ by = date[2]["by"]
92
+ call = date[2]["call"]
93
+ send_to_channel(f"広告:{text}")
94
+ send_to_channel(f"{by}様より。call:{call}")
95
+ except Exception as e:
96
+ log(f"広告{rand}番エラー:{e}",3)
97
+
98
+
99
+
100
+
101
+
102
+ def get_messages():
103
+ try:
104
+ res = requests.get(
105
+ MESSAGES_URL,
106
+ headers=HEADERS,
107
+ params=PARAMS,
108
+ timeout=30,
109
+ )
110
+ res.raise_for_status()
111
+ return res.json().get("messages", [])
112
+
113
+ except Exception as e:
114
+ log(f"MSG取得関数に失敗:{e}",3)
115
+
116
+ def send_to_channel(text):
117
  try:
118
  payload = {
119
  "requestId": f"desk-web-{int(time.time() * 1000)}",
120
  "blocks": [
121
  {
122
+ "type": "text",
123
  "value": text
124
  }
125
+ ],
126
  }
127
 
128
  res = requests.post(
129
  MESSAGES_URL,
130
  headers=HEADERS,
131
  data=json.dumps(payload),
132
+ timeout=30,
133
  )
134
  res.raise_for_status()
135
+
136
+ except Exception as e:
137
+ log(f"MSG送信関数に失敗:{e}",1)
138
+
139
+ #def send_to_channel_file(file_url):
140
+ # try:
141
+ # #get_request送る->https://files.electrohaxz.host/にupする->リンクをfile_urlに入れて送る。
142
+ # payload = {
143
+ # "requestId": f"desk-web-{int(time.time() * 1000)}",
144
+ # "blocks": [
145
+ # {
146
+ # "type": "text",
147
+ # "value": file_url
148
+ # }
149
+ # ],
150
+ # }
151
+ #
152
+ # res = requests.post(
153
+ # MESSAGES_URL,
154
+ # headers=HEADERS,
155
+ # data=json.dumps(payload),
156
+ # timeout=30,
157
+ # )
158
+ # res.raise_for_status()
159
+ #
160
+ # except Exception as e:
161
+ # log(f"ファイルアップロード関数に失敗:{e}",1)
162
+ # ===== Main Loop =====
163
 
164
+ log("BOTのメインループを開始します。",1)
165
+ def main():
166
+ while True:
167
+ try:
168
+ NEXT_SEND = 180
169
+ while NEXT_SEND > 0:
170
+ log(f"次回まであと、{NEXT_SEND}秒",1)
171
+ NEXT_SEND = NEXT_SEND - 1
172
+ time.sleep(1)
173
+ send_to_channel(f"浮上。つまり、私がキタ━━━━(゚∀゚)━━━━!!。")
174
+ time_datetime= datetime.now()
175
+ send_to_channel(f"現在時刻は多分+UTCで{time_datetime}です!")
176
+ time.sleep(1)
177
+ ramdom_msg()
178
+ send_to_channel(f"\(^^)/眠くなったからおやすみ。また時報&宣伝します。\(^^)/")
179
 
180
+ except Exception as e:
181
+ log(f"メインループ:{e}",3)
182
+
 
183
 
 
184
  if __name__ == "__main__":
185
+ main()