forked from wiai/doorbot
* wrap TemporaryFile in context
* add logging * fix plot configuration * add configurable schedule for post_plotmaster
parent
7641a9d130
commit
87b6615793
|
|
@ -0,0 +1,3 @@
|
|||
settings.json
|
||||
__pycache__/
|
||||
*.pyc
|
||||
23
bot.py
23
bot.py
|
|
@ -3,10 +3,13 @@ import datetime
|
|||
import json
|
||||
import time
|
||||
from tempfile import NamedTemporaryFile
|
||||
import logging
|
||||
|
||||
import requests
|
||||
import schedule
|
||||
|
||||
logging.basicConfig(format='%(levelname)s %(name)s:%(message)s', level=logging.DEBUG)
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
def parse_time(string):
|
||||
return datetime.datetime.strptime(string, "%Y-%m-%d %H:%M:%S")
|
||||
|
|
@ -26,26 +29,31 @@ def get_status_text(src=get_status):
|
|||
def post(chats, text, token):
|
||||
url = "https://api.telegram.org/bot{token}/sendMessage".format(token=token)
|
||||
for chat in chats:
|
||||
requests.post(url, data={'chat_id': chats[chat], "text": text})
|
||||
response = requests.post(url, data={'chat_id': chats[chat], "text": text})
|
||||
log.info("post message: %s", response.status_code)
|
||||
|
||||
def main(args={"config": "settings.json"}):
|
||||
text = get_status_text()
|
||||
log.info("run once")
|
||||
config = json.load(open(args['config']))
|
||||
text = get_status_text()
|
||||
post(config['groups'], text, config['token'])
|
||||
post_plot(config)
|
||||
|
||||
def loop(args={"config": "settings.json"}):
|
||||
log.info("prepare loop")
|
||||
config = json.load(open(args['config']))
|
||||
setup(config)
|
||||
while True:
|
||||
try:
|
||||
do_loop(config)
|
||||
except Exception as e:
|
||||
print(e, e.args)
|
||||
log.exception(e)
|
||||
time.sleep(config['sleep'])
|
||||
|
||||
def do_loop(config):
|
||||
last_state = None
|
||||
while True:
|
||||
log.info("enter loop")
|
||||
changed = False
|
||||
new_state = get_status()
|
||||
if last_state is None:
|
||||
|
|
@ -56,22 +64,24 @@ def do_loop(config):
|
|||
last_state = new_state
|
||||
text = get_status_text(lambda: last_state)
|
||||
post(config["groups"], text, config["token"])
|
||||
log.info("run pending tasks")
|
||||
schedule.run_pending()
|
||||
log.info("sleep")
|
||||
time.sleep(config['sleep'])
|
||||
|
||||
def post_plot(config):
|
||||
from plot import get_plot
|
||||
target= NamedTemporaryFile()
|
||||
with NamedTemporaryFile() as target:
|
||||
image_url = 'https://api.telegram.org/bot{token}/sendPhoto'.format(token=config['token'])
|
||||
files = {'photo': get_plot(target)}
|
||||
for chat in config['groups']:
|
||||
files['photo'].seek(0)
|
||||
values = {"chat_id":config['groups'][chat]}
|
||||
r = requests.post(image_url, files=files, data=values)
|
||||
print(r)
|
||||
log.info("post photo: %s", r.status_code)
|
||||
|
||||
def setup(config):
|
||||
schedule.every(30).seconds.do(lambda: post_plot(config))
|
||||
schedule.every(config['photo_interval']).seconds.do(lambda: post_plot(config))
|
||||
#schedule.every().week.do(post_plot)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
@ -79,7 +89,6 @@ if __name__ == "__main__":
|
|||
parser.add_argument("--config", "-c", default="settings.json", help="Configuration file")
|
||||
parser.add_argument("--loop", "-l", action="store_true", help="Loop")
|
||||
args = parser.parse_args()
|
||||
setup(vars(args))
|
||||
if args.loop:
|
||||
loop(vars(args))
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
requests
|
||||
schedule
|
||||
requests==2.18.4
|
||||
schedule==0.4.3
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
{
|
||||
"token": "BOTID:TOKEN",
|
||||
"sleep": 30,
|
||||
"photo_interval": 30,
|
||||
"groups":{
|
||||
"wiaidoor": -12357567
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue