Beispiel #1
0
 def setteacher_id(self):
     c = Chat(chat_id=self.chat_id,
              group_id=-1,
              teacher_id=self.teacher_id)
     c.save()
     reply(self.chat_id, msg=self.responses['setteacher_success'])
Beispiel #2
0
def index(request):
    chat = Chat()
    message = ""
    chat_id = ""
    try:
        data = json.loads(request.body.decode("utf-8"))
        chat_id = data["message"]["chat"]["id"]
        user_id = data["message"]["from"]["id"]
        message = data["message"]["text"]
        chat = Chat.objects.get(pk=chat_id)
    # If chat not in database
    except Chat.DoesNotExist:
        chat = Chat(chat_id=chat_id)
        chat.save()
    except Exception:
        return HttpResponse()

    # Set user language
    responses = ru if chat.language == "ru" else ua
    # Make commands and parameters case insensitive
    message = message.lower()

    try:
        # Check command existance
        command = message.split()[0].split("@")[0]
        if command not in commands:
            return HttpResponse()

        # Statistics
        track(miscellaneous.key.BOTAN_TOKEN, user_id, {get_group_name_by_id(chat.group_id): 1}, "Group")
        track(miscellaneous.key.BOTAN_TOKEN, user_id, {}, command)

        # If command doesn't need timetable
        if command == "/start" or command == "/help":
            reply(chat_id, msg=responses["instructions"])
        elif command == "/authors":
            reply(chat_id, msg=responses["authors"])
        elif command == "/week":
            reply(chat_id, msg=responses["week"].format(2 - datetime.date.today().isocalendar()[1] % 2))
        elif command == "/time":
            reply(chat_id, msg=time)
        elif command == "/changelang":
            if chat.language == "ru":
                chat.language = "ua"
                reply(chat_id, msg=ua["change_lang"])
            else:
                chat.language = "ru"
                reply(chat_id, msg=ru["change_lang"])
            chat.save()

        if command in no_timetable_commands:
            return HttpResponse()

        # If command require timetable
        tt = TeacherTimetable(chat_id, message) if chat.group_id == -1 else GroupTimetable(chat_id, message)

        # Check wrong parameter and access error
        if tt.is_wrong_parameter:
            return HttpResponse()

        # Command processing
        getattr(tt, command[1:])()
    except:
        pass
    finally:
        return HttpResponse()
Beispiel #3
0
 def setgroup(self):
     c = Chat(chat_id=self.chat_id,
              group_id=self.group_id,
              teacher_id=0)
     c.save()
     reply(self.chat_id, msg=self.responses['setgroup_success'])