File size: 1,354 Bytes
5db4f1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
from dotenv import load_dotenv

import client_config as usrcfg

load_dotenv()

MONITOR_SERVER_INTERFACE = os.environ.get("HOST", "localhost")
MONITOR_SERVER_PORT = int(os.environ.get("PORT", 50001))
CHANNEL_IP = f"{MONITOR_SERVER_INTERFACE}:{MONITOR_SERVER_PORT}"

AUTHORIZATION = os.environ.get("STORYTELLINGS_AUTH")

class BotScriptGetter:
    @classmethod 
    def from_url(cls, url, script_type, variables):
        response = requests.get(url, headers={'Authorization': AUTHORIZATION})
        data = response.text.encode("utf-8")
        
        return data, script_type, variables
        
    @classmethod
    def from_file(cls, filename, script_type, variables):
        with open(filename, 'r') as f:
            data = f.read()
            data = data.encode("utf-8")
            
        return data, script_type, variables
    
    @classmethod
    def from_scenario(cls, name):
        script_config = usrcfg.script_and_config.get(name, {})
        
        script_type = script_config.get("type")
        
        variables = script_config.get("var", {})
        
        if "url" in script_config:
            return cls.from_url(script_config.get("url"), script_type, variables)
        
        elif "path" in script_config:
            return cls.from_file(script_config.get("path"), script_type, variables)