File size: 5,147 Bytes
0e70376
 
31f1786
 
0e70376
31f1786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70ac8af
 
31f1786
 
 
 
 
0e70376
 
31f1786
0e70376
31f1786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e70376
31f1786
0e70376
 
 
31f1786
0e70376
 
 
 
31f1786
 
 
 
 
 
 
 
 
 
70ac8af
 
 
31f1786
 
70ac8af
31f1786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ff6b83
 
 
 
 
31f1786
6ff6b83
 
31f1786
6ff6b83
 
 
 
 
 
31f1786
6ff6b83
 
31f1786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e70376
31f1786
 
 
 
81c1f3d
31f1786
 
 
 
 
 
 
 
 
 
6ff6b83
31f1786
 
 
0e70376
 
31f1786
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import time
from datetime import datetime
import json
import requests
import traceback
import random



def log(msg,type):
    if type == 1:#通常ログ
        time_datetime= datetime.now() 
        print(f"{time_datetime}[log]:{msg}")
    elif type == 2: #警告
        time_datetime= datetime.now() 
        print(f"{time_datetime}[warn]:{msg}")
    elif type == 3: #エラー
        time_datetime= datetime.now() 
        print(f"{time_datetime}[error]:{msg}")
        traceback.print_exc()


def System_up(sec):
    START_TIME = sec
    while START_TIME > 0:
        log(f"起動まで、{START_TIME}秒",1)
        START_TIME = START_TIME - 1
        time.sleep(1)



System_up(3)


log("起動しました。",1)


# ===== Channel.io 設定 =====
CHANNEL_ID = os.getenv("CHIO_CH_ID")#"200605" チャンネル
if not CHANNEL_ID:
    raise RuntimeError("環境変数 CHIO_CH_ID が設定されていません")

GROUP_ID = os.getenv("CHIO_GR_ID")#"534868" グループ
if not GROUP_ID:
    raise RuntimeError("環境変数 CHIO_GR_ID が設定されていません")

MESSAGES_URL = f"https://desk-api.channel.io/desk/channels/{CHANNEL_ID}/groups/{GROUP_ID}/messages"

X_ACCOUNT = os.getenv("CHIO_TOKEN") #トークン
if not X_ACCOUNT:
    raise RuntimeError("環境変数 CHIO_TOKEN が設定されていません")

HEADERS = {
    "accept": "application/json",
    "accept-language": "ja",
    "content-type": "application/json",
    "x-account": X_ACCOUNT,
}

PARAMS = {
    "sortOrder": "desc",
    "limit": 50,
}

# ===== Utils =====

def ramdom_msg():
    date = [
        {"no":0,"by":"name","text":"message","call":"addres"},#0
        {"no":1,"by":"name_00","text":"message","call":"addres"},#0
        {"no":2,"by":"name_01","text":"message","call":"addres"},#0
        {"no":3,"by":"name_02","text":"message","call":"addres"},#0

    ]
    rand = random.randint(0, 3)#ランダム最大値指定。
    if rand == 0:
        ramdom_msg()#再起
        log("辞書の0が指定されたから無視",1)
    elif rand == 1:
        try:
            text = date[1]["text"]
            by = date[1]["by"]
            call = date[1]["call"]
            send_to_channel(f"広告:{text}")
            send_to_channel(f"{by}様より。call:{call}")
        except Exception as e:
            log(f"広告{rand}番エラー:{e}",3)
    elif rand == 2:
        try:
            text = date[2]["text"]
            by = date[2]["by"]
            call = date[2]["call"]
            send_to_channel(f"広告:{text}")
            send_to_channel(f"{by}様より。call:{call}")
        except Exception as e:
            log(f"広告{rand}番エラー:{e}",3)
   




def get_messages():
    try:
        res = requests.get(
                MESSAGES_URL,
                headers=HEADERS,
                params=PARAMS,
                timeout=30,
            )
        res.raise_for_status()
        return res.json().get("messages", [])
    
    except Exception as e:
        log(f"MSG取得関数に失敗:{e}",3)

def send_to_channel(text):
    try:
        payload = {
            "requestId": f"desk-web-{int(time.time() * 1000)}",
            "blocks": [
                {
                    "type": "text", 
                    "value": text
                }
            ],
        }
    
        res = requests.post(
            MESSAGES_URL,
            headers=HEADERS,
            data=json.dumps(payload),
            timeout=30,
        )
        res.raise_for_status()
        
    except Exception as e:
        log(f"MSG送信関数に失敗:{e}",1)

#def send_to_channel_file(file_url):
#   try:
#        #get_request送る->https://files.electrohaxz.host/にupする->リンクをfile_urlに入れて送る。
#        payload = {
#            "requestId": f"desk-web-{int(time.time() * 1000)}",
#            "blocks": [
#                {
#                    "type": "text", 
#                    "value": file_url
#                }
#            ],
#        }
#    
#        res = requests.post(
#            MESSAGES_URL,
#            headers=HEADERS,
#            data=json.dumps(payload),
#            timeout=30,
#        )
#        res.raise_for_status()
#        
#   except Exception as e:
#        log(f"ファイルアップロード関数に失敗:{e}",1)
# ===== Main Loop =====

log("BOTのメインループを開始します。",1)
def main():
    while True:
        try:
            NEXT_SEND = 120     
            while NEXT_SEND > 0:
                log(f"次回まであと、{NEXT_SEND}秒",1)
                NEXT_SEND = NEXT_SEND - 1
                time.sleep(1)
            send_to_channel(f"浮上。つまり、私がキタ━━━━(゚∀゚)━━━━!!。")
            time_datetime= datetime.now()
            send_to_channel(f"現在時刻は多分+UTCで{time_datetime}です!")
            time.sleep(1)
            ramdom_msg()
            send_to_channel(f"\(^^)/眠くなったからおやすみ。また時報&宣伝します。\(^^)/")
        
        except Exception as e:
            log(f"メインループ:{e}",3)


if __name__ == "__main__":
    main()