Beispiel #1
0
 def sendOnlineMessages(data, lastData):
     offline = list()
     names = DbApi.subscribedUsers()
     names = [x[0] for x in names]
     for name in names:
         if name not in [x['username'] for x in data
                         ] and lastData is not None and name in [
                             x['username'] for x in lastData
                         ]:
             Send.sendOnlineMsg(name, "offline")
             offline += name
     on = [
         x for x in data
         if x['username'] in names and x['username'] not in offline
     ]
     for member in on:
         try:
             oldMember = DbApi.lastState(member["username"])
             if lastData is None or member["username"] not in [
                     x['username'] for x in lastData
             ] or oldMember["status"] != member["status"]:
                 Send.sendOnlineMsg(member["username"], member["status"])
         except Exception as e:
             print(f'Online Message failed because of [{str(e)}]')
     if (lastData is None
             or not any([x["channel_id"] == None
                         for x in lastData])) and any(
                             [x["channel_id"] != None for x in data]):
         Send.sendOnlineMsg(
             str(sum([x["channel_id"] == None for x in data])),
             "in channel")
Beispiel #2
0
def mainloop():
    print(f"Starting netMonitor in {CONFIG['env']}")
    dbApi = DbApi()
    send = Send()
    send.sendInitial(f"Starting netMonitor in {CONFIG['env']}")
    netApi = NetApi()
    send.sendError(dbApi.test())
    lastTimestamp = None
    while True:
        try:
            currtime = time.time()
            distance = 300 - currtime % 300
            alligned_time = currtime + distance
            time.sleep(distance)  # wait to the next 5 min
            timestamp = datetime.datetime.fromtimestamp(
                int(alligned_time))  # everyone gets same 5min alligned time
            try:
                data = netApi.getData()
                dbApi.dbCollect(data, timestamp)
                try:
                    send.sendCheck(dbApi, lastTimestamp, timestamp)
                except Exception as e:
                    send.sendError(
                        f"Possible sending messages unsuccesful, because of {str(e)}"
                    )
                lastTimestamp = timestamp
            except Exception as e:
                send.sendError(
                    f"DataCollection unsuccesful, because of {str(e)}")
        except Exception as e:
            send.sendError(f"Fail in Mainloop pending 290s, because of {e}")
            time.sleep(290)
Beispiel #3
0
def status(update, context):
    chat_id = update.effective_chat.id
    if chat_id == CONFIG["chat_id"]:
        msg = DbApi.adminStatus()
        context.bot.send_message(text=msg, chat_id=chat_id)
    else:
        msg = DbApi.status()
        context.bot.send_message(text=msg, chat_id=chat_id)
Beispiel #4
0
def all(update, context):
    chat_id = update.effective_chat.id
    if allowed(chat_id):
        if chat_id == CONFIG["admin_chat_id"]:
            dbApi = DbApi()
            msg = dbApi.getAllDevices()
            context.bot.send_message(text=msg, chat_id=chat_id)
        else:
            msg = "Oh this is not the right channel"
            context.bot.send_message(text=msg, chat_id=chat_id)
Beispiel #5
0
def unsubscribe(update, context):
    chat_id = update.effective_chat.id
    text = update.message.text
    if "/unsubscribe " in text:
        name = text.replace("/unsubscribe ", "")
        if name:
            try:
                DbApi.rmSubscriber(chat_id, name)
                msg = f"Unsubscribed from: {name}"
            except Exception as ex:
                msg = f"Could not unsubscribe from: {name}\nCheck your /subsribtions" + f"\n{CONFIG['env']}: because of {ex}" if CONFIG[
                    "env"] == "dev" else ""
        else:
            msg = "No Name Provided"
    else:
        msg = "No Name Provided"
    context.bot.send_message(chat_id=chat_id, text=msg)
Beispiel #6
0
 def sendOnlineMsg(name, status):
     bot = telegram.Bot(token=CONFIG["bot_token"])
     subscribers = DbApi.subscribers(name)
     msg = f'{name} now {status}'
     print(msg, subscribers)
     for chat_id in subscribers:
         try:
             bot.sendMessage(chat_id=chat_id, text=msg)
         except Exception as e:
             print(f'Online Message failed because of [{str(e)}]')
Beispiel #7
0
def subscribe(update, context):
    chat_id = update.effective_chat.id
    if allowed(chat_id):
        text = update.message.text
        if "/subscribe " in text:
            name = text.replace("/subscribe ", "")
            if name:
                try:
                    dbApi = DbApi()
                    dbApi.newSubscriber(chat_id, name)
                    msg = f"Subscribed to: {name}"
                except Exception as ex:
                    msg = f"Could not subscribe to: {name}\nCheck your /subsribtions" + f"\n{CONFIG['env']}: because of {ex}" if CONFIG[
                        "env"] == "dev" else ""
            else:
                msg = "No Name Provided"
        else:
            msg = "No Name Provided"
        context.bot.send_message(chat_id=chat_id, text=msg)
Beispiel #8
0
def auth(update, context):
    chat_id = update.effective_chat.id
    text = update.message.text
    username = update.effective_user.username
    first_name = update.effective_user.first_name
    last_name = update.effective_user.last_name
    if "/auth " in text:
        auth = text.replace("/auth ", "")
        if auth == CONFIG["auth"]:
            try:
                dbApi = DbApi()
                dbApi.authenticate(chat_id, username, first_name, last_name)
                msg = "Success! Please delete your message!"
            except Exception as ex:
                msg = f"Could not auth" + f"\n{CONFIG['env']}: because of {ex}" if CONFIG[
                    "env"] == "dev" else ""
        else:
            msg = "No right Auth Provided"
    else:
        msg = "No right Auth Provided"
    context.bot.send_message(chat_id=chat_id, text=msg)
Beispiel #9
0
def subscribtions(update, context):
    chat_id = update.effective_chat.id
    msg = str([x[0] for x in DbApi.chatSubscribedUsers(chat_id)])
    context.bot.send_message(chat_id=chat_id, text=msg)
Beispiel #10
0
def allowed(chat_id):
    dbApi = DbApi()
    return dbApi.authenticated(chat_id) or chat_id == CONFIG["admin_chat_id"]