-
Notifications
You must be signed in to change notification settings - Fork 10
/
pybot
executable file
·64 lines (49 loc) · 1.66 KB
/
pybot
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Main script for starting pybot.
Usage:
pybot telegram [--token=<token>][--tokenfile=<tokenfile>]
pybot (-h | --help)
Options:
-h --help Show this screen.
"""
import os
import sys
import configparser
from docopt import docopt
PARENT_FOLDER = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
if os.path.isdir(os.path.join(PARENT_FOLDER, '.venv')):
sys.path.insert(0, PARENT_FOLDER)
else:
sys.path.append('../pybot')
from pybot.interfaces import telegram
CONFIG_FILE = os.path.join(PARENT_FOLDER, 'conf/pybot.conf')
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
def read_firsline(filepath):
"Read first line from a file"
try:
return open(filepath, 'r').readline().splitlines()[0]
except OSError:
print('Unable to open file', filepath)
raise SystemExit
def set_env_vars():
"Set environment variables from the config file"
for varname in config['env_vars']:
os.environ[varname.upper()] = config.get('env_vars', varname)
if __name__ == "__main__":
args = docopt(__doc__)
set_env_vars()
if args['telegram']:
if args['--token']:
TOKEN = args['--token']
print('Starting pybot using token. CTRL-C to quit.')
elif args['--tokenfile']:
TOKEN = read_firsline(args['--tokenfile'])
print('Starting pybot using argument token file. CTRL-C to quit.')
else:
TOKEN = read_firsline(config.get('telegram', 'tokenfile'))
print('Starting pybot using conf token file. CTRL-C to quit.')
os.environ["TELEGRAM_TOKEN"] = TOKEN
telegram.start()