File size: 1,565 Bytes
75f35c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import requests
import os
#汎用チャンネルトーク関連。

def chio_set(ch_id,gr_id,chio_token)
    CHANNEL_ID = ch_id#"200605" チャンネル
    if not CHANNEL_ID:
        raise RuntimeError("環境変数 CHIO_CH_ID が設定されていません")
    
    GROUP_ID = 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 = 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,
    }

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:
        time_datetime = datetime.now()    
        print(f"{time_datetime}[error]:message送信に失敗しました。:{e}")