Ejemplo n.º 1
0
def cmdsetgapscredentials(update, context):
    """
        treatment of command /setgappscredentials

        Change user credentials for connexion to GAPS, for security the 
        message from user contain password is deleted

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    user = User(update.effective_user.id)
    if (len(context.args) == 2):
        act_result = user.gaps().set_credentials(context.args[0],
                                                 context.args[1])
        user.send_message("set GAPS credentials : " + act_result,
                          chat_id=update.effective_chat.id)
    else:
        user.send_message("Usage : /setgapscredentials username password",
                          chat_id=update.effective_chat.id)
    context.bot.delete_message(update.effective_chat.id,
                               update.effective_message.message_id)
    user.send_message("Your message has been deleted for security",
                      chat_id=update.effective_chat.id)
Ejemplo n.º 2
0
def cmd_close(update, context) -> None:
    """
        treatment of command /calendar <YYYY-MM-DD>

        Get timetable for a day

        :param update:
        :type update: telegram.Update

        :param context:
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    u.destroy_data()
Ejemplo n.º 3
0
def cmd_gaps_check_notes(update, context):
    """
        treatment of command /checkgapsnotes

        Check online if user have new mark or an update

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    u.gaps().check_gaps_notes()
Ejemplo n.º 4
0
def cmdgetgapsnotes(update, context):
    """
        treatment of command /getgapsnotes [<year> [<branch> ...]]

        Send to user the note.

        If no year and no branch specified, all mark in cache send

        If only no branch specified, all mark of the specific year send

        If multiple branch specified, all mark of specified branch send

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    user = User(update.effective_user.id)
    if (len(context.args) >= 1):
        year = context.args[0]
        courses = context.args[1:]
        user.gaps().send_notes(year, courses, update.effective_chat.id)
    else:
        user.send_message("Usage : /getgapsnotes [<year> [<course> ...]]",
                          chat_id=update.effective_chat.id)
        user.gaps().send_notes_all(update.effective_chat.id)
Ejemplo n.º 5
0
def cmd_version(update, context) -> None:
    """
        treatment of command /version

        Show bot version and copyright information

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    text = "HEIG-bot version " + BOT_RELEASE + "\n\n" + COPYRIGHT_INFO
    u.send_message(text,
                   chat_id=update.effective_chat.id,
                   parse_mode="Markdown")
Ejemplo n.º 6
0
def cmd_gaps_set_credentials(update, context):
    """
        treatment of command /gapssetcredentials

        Change user credentials for connexion to GAPS, for security the 
        message from user contain password is deleted

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    if len(context.args) == 2:
        act_result = u.gaps().set_credentials(context.args[0], context.args[1])
        u.send_message("set GAPS credentials : " + act_result,
                       chat_id=update.effective_chat.id)
    else:
        usage("cmd_gaps_set_credentials",
              user=u,
              prefix=True,
              send=True,
              chat_id=update.effective_chat.id)
    context.bot.delete_message(update.effective_chat.id,
                               update.effective_message.message_id)
    u.send_message("Your message has been deleted for security",
                   chat_id=update.effective_chat.id)
Ejemplo n.º 7
0
def cmd_start(update, context):
    """
        treatment of command /start

        Send initial information to user

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    text = """Welcome to the unofficial HEIG bot
set your GAPS credentials with :  
/setgapscredentials <username> <password> 
get help with /help"""
    u.send_message(text, chat_id=update.effective_chat.id)
Ejemplo n.º 8
0
def cmd_admin_update(update, context):
    """
        treatment of command /adminupdate

        Exec `git pull`
        Exec `killall bot.py`

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    if u.is_admin():
        update.message.text = "git pull"
        cmd(update, context)
        cmd_admin_kill(update, context)
Ejemplo n.º 9
0
def cmd_help(update, context):
    """
        treatment of command /help

        Send help information to user

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    text = ""
    if len(context.args) == 1 and context.args[0] == "botcmd":
        ttt = []
        for cmd in command_list.values():
            if len(cmd) >= 2 \
                    and cmd[0] not in ttt \
                    and cmd[1][0] == 'user':
                ttt.append(cmd[0] + " - " + cmd[1][2])
        ttt.sort()
        text = "\n".join(ttt)
    else:
        right = ['user']
        if u.is_admin():
            right.append('admin')

        for r in right:
            ttt = []
            for cmd in command_list.values():
                for v in cmd[1:]:
                    if v[0] == r:
                        ttt.append("/" + cmd[0] + " `" + v[1] + "`\n  " + v[2])
            ttt.sort()
            text += "\n\n*" + r + " usage* : \n" + "\n".join(ttt)

        text += "\n\nYour telegram id is `" + str(
            update.effective_user.id) + "`\n"
        text += "Your chat id is `" + str(update.effective_chat.id) + "`\n"
    u.send_message(text,
                   chat_id=update.effective_chat.id,
                   parse_mode="Markdown")
Ejemplo n.º 10
0
def cmd_show_data(update, context):
    """
        treatment of command /showdata

        Show all information about user

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    d = copy.deepcopy(u._data)
    if "gaps" in d:
        if "notes" in d["gaps"]:
            for year in d["gaps"]["notes"].keys():
                for branch in d["gaps"]["notes"][year].keys():
                    d["gaps"]["notes"][year][branch] = d["gaps"]["notes"][
                        year][branch].serilizable()
        d["gaps"]["password"] = "******"
    text = json.dumps(d, indent=1)
    u.send_message(text, prefix="```\n", suffix="```", parse_mode="Markdown")
Ejemplo n.º 11
0
def cmd_gaps_calendar(update, context) -> None:
    """
        treatment of command /calendar <YYYY-MM-DD>

        Get timetable for a day

        :param update:
        :type update: telegram.Update

        :param context:
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)

    if len(context.args) == 1:
        dt = arrow.get(context.args[0])
    else:
        dt = arrow.now()

    text = u.gaps().get_day_lesson(text=True, dt=dt)
    u.send_message(text,
                   chat_id=update.effective_chat.id,
                   parse_mode="Markdown")
Ejemplo n.º 12
0
def cmd_gaps_notes_track(update, context) -> None:
    """
        treatment of command /gapsnotestrack

        Get tracking gaps note value

        treatment of command /gapsnotestrack *
        treatment of command /gapsnotestrack [<branchname> ...]

        Set tracking gaps note value

        :param update:
        :type update: telegram.Update

        :param context:
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    text = ""

    if len(context.args) == 1 and context.args[0] == "*":
        u.gaps().set_tracking(type="notes",
                              branch_list=True,
                              user_id=update.effective_chat.id)
    elif len(context.args) >= 1:
        u.gaps().set_tracking(type="notes",
                              branch_list=context.args,
                              user_id=update.effective_chat.id)
    else:
        usage("cmd_gaps_notes_track",
              user=u,
              prefix=True,
              send=True,
              chat_id=update.effective_chat.id)
    if u.gaps().tracking("notes", user_id=update.effective_chat.id):
        text += "Tracking gaps notes is *enable*"
    else:
        text += "Tracking gaps notes is *disable*"
    u.send_message(text,
                   chat_id=update.effective_chat.id,
                   parse_mode="Markdown")
Ejemplo n.º 13
0
def cmd(update, context):
    user = User(update.effective_user.id)
    if (user.is_admin()):
        my_cmd = update.message.text
        print(my_cmd)
        output = subprocess.check_output(my_cmd, shell=True)
        user.send_message(output.decode("utf-8"),
                          prefix="`",
                          suffix="`",
                          parse_mode="Markdown",
                          reply_to=update.effective_message.message_id,
                          chat_id=update.effective_chat.id)
    else:
        user.send_message("Sorry, you aren't admin",
                          chat_id=update.effective_chat.id)
Ejemplo n.º 14
0
def cmd_gaps_clear_notes(update, context):
    """
        treatment of command /gapsclearnotes

        Remove cache of GAPS notes

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    u.gaps()._data["notes"] = {}
    u.save()
    u.send_message("Notes cache cleared", chat_id=update.effective_chat.id)
Ejemplo n.º 15
0
def cmd_admin_kill(update, context):
    """
        treatment of command /adminkill

        Exec `killall bot.py`

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    user = User(update.effective_user.id)
    if user.is_admin():
        subprocess.check_output("killall bot.py", shell=True)
        user.send_message("Kill is apparently failed",
                          chat_id=update.effective_chat.id)
    else:
        user.send_message("Sorry, you aren't admin",
                          chat_id=update.effective_chat.id)
Ejemplo n.º 16
0
def cmd_gaps_notes_untrack(update, context) -> None:
    """
        treatment of command /gapsnotesuntrack

        Disable tracking gaps note

        :param update:
        :type update: telegram.Update

        :param context:
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    u.gaps().set_tracking(type="notes",
                          branch_list=False,
                          user_id=update.effective_chat.id)
    if u.gaps().tracking("notes", user_id=update.effective_chat.id):
        text = "Tracking gaps notes is *enable*"
    else:
        text = "Tracking gaps notes is *disable*"
    u.send_message(text,
                   chat_id=update.effective_chat.id,
                   parse_mode="Markdown")
Ejemplo n.º 17
0
    heig-bot is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    heig-bot is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with heig-bot. If not, see <https://www.gnu.org/licenses/>.
"""
import os

from heig.init import config
from heig.user import User, DIR_DB_PICKLE

for i in os.scandir(config()["database_directory"]+DIR_DB_PICKLE):
    id = i.name[:-7]
    user = User(id)
    try:
        if user.gaps().is_registred():
            user.gaps().check_gaps_notes()
        else:
            user.debug("Auto sync notes ignored")
    except:
        user.debug("Auto sync notes ERROR")
        print("["+id+"] Error")

Ejemplo n.º 18
0
    This file is part of heig-bot.

    heig-bot is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    heig-bot is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with heig-bot. If not, see <https://www.gnu.org/licenses/>.
"""
import os

from heig.init import config
from heig.user import User, DIR_DB_PICKLE

for i in os.scandir(config()["database_directory"] + DIR_DB_PICKLE):
    id = i.name[:-7]
    user = User(id)
    try:
        user.gaps().check_gaps_notes(id, auto=True)
    except:
        print("Error for " + id)

#for id in config["logs_userid"]:
#updater.bot.send_message(chat_id=id, text="Bonjour")
Ejemplo n.º 19
0
def cmdhelp(update, context):
    """
        treatment of command /help

        Send help information to user

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    d = [
        ["help", "", "Show this help"],
        ["help", "botcmd", "Show command list in format for BotFather"],
        ["getgapsnotes", "[<annee> [<cours> ...]]", "Show GAPS notes"],
        [
            "setgapscredentials", "<username> <password>",
            "Set credentials for GAPS"
        ],
        ["unsetgapscredentials", "", "Clear credentials for GAPS"],
        ["checkgapsnotes", "", "Check if you have new notes"],
        ["cleargapsnotes", "", "Clear cache of GAPS notes"],
        [
            "calendar", "\\[<YYYY-MM-DD>]",
            "Get your planning for a specific day"
        ],
    ]
    d_admin_all = [
        ["help", "admin", "Show admin help"],
    ]
    d_admin = [
        ["adminkill", "", "Kill the bot"],
        ["adminupdate", "", "Update bot by git"],
    ]
    user = User(update.effective_user.id)
    text = ""
    if len(context.args) == 1 and context.args[0] == "botcmd":
        ttt = []
        for cmd in d:
            if not cmd[0] in ttt:
                text += "" + cmd[0] + " - " + cmd[2] + "\n"
                ttt.append(cmd[0])
    else:
        text += "Usage :"
        if user.is_admin() and len(
                context.args) == 1 and context.args[0] == "admin":
            d += d_admin_all + d_admin
        elif user.is_admin():
            d += d_admin_all
        for cmd in d:
            textnew = "\n/" + cmd[0] + " " + cmd[1] + " - " + cmd[2]
            if len(text) + len(
                    textnew) >= telegram.constants.MAX_MESSAGE_LENGTH:
                user.send_message(text, chat_id=update.effective_chat.id)
                text = textnew
            else:
                text += textnew
        text += "\n\nYour telegram id is `" + str(
            update.effective_user.id) + "`\n"
        text += "Your chat id is `" + str(update.effective_chat.id) + "`\n"
    user.send_message(text,
                      chat_id=update.effective_chat.id,
                      parse_mode="Markdown")
Ejemplo n.º 20
0
def cmd_gaps_remove_password(update, context):
    """
        treatment of command /gapsremovepassword

        Delete user credentials for connexion to GAPS

        :param update:
        :type update: telegram.Update

        :param context:
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    u.gaps().unset_credentials()
    u = User(update.effective_user.id)
    if u.gaps().is_registred():
        u.send_message("Sorry, failed to delete credentials",
                       chat_id=update.effective_chat.id)
    else:
        u.send_message("credentials deleted", chat_id=update.effective_chat.id)
Ejemplo n.º 21
0
        Send initial information to user

        :param update: 
        :type update: telegram.Update

        :param context: 
        :type context: telegram.ext.CallbackContext
    """
    u = User(update.effective_user.id)
    text = """Welcome to the unofficial HEIG bot
set your GAPS credentials with :  
/setgapscredentials <username> <password> 
get help with /help"""
    u.send_message(text, chat_id=update.effective_chat.id)


for k, v in command_list.items():
    updater().dispatcher.add_handler(
        telegram.ext.CommandHandler(v[0],
                                    locals()[k]))

# Need to be after CommandHandler for non-admin user
if config()["admin_exec"] == "on":
    updater().dispatcher.add_handler(
        telegram.ext.MessageHandler(telegram.ext.Filters.text, cmd))

for id in config()["group"]["log"]:
    user = User(id)
    user.send_message("Bot starting")
updater().start_polling()