#author : @thematrixsimulation

import logging
from configparser import RawConfigParser
import os
import requests
import json

config_object = RawConfigParser()
scriptDirectory = os.path.dirname(os.path.realpath(__file__))
settingsFilePath = os.path.join(scriptDirectory, ".config.ini")
config_object.readfp(open(settingsFilePath,"r"))

botinfo = config_object["botinfo"]
telegram_id = botinfo["telegram_id"]
telegram_chat_id = botinfo["telegram_chat_id"]

def send_telegram_message(message):
    """Sends alert via Telegram"""
    url = "https://api.telegram.org/" + telegram_id + "/sendMessage"
    data = {
        "chat_id": telegram_chat_id,
        "text": message
    }
    try:
        response = requests.request(
            "POST",
            url,
            params=data
        )
        logging.info(response.text)
        telegram_data = json.loads(response.text)
        return telegram_data["ok"]
    except Exception as e:
        logging.info(f"failed with error {e}")
        return False
