示例#1
0
def forward_sms(request: HttpRequest) -> HttpResponse:
    """
    Forwards an SMS message to a user.

    @endpoint /forward
    @param    ?code=<code>
    @param    &username=<username>
    @params   [&{address, body, date}]
    """
    # Check for required params
    if "code" not in request.GET:
        return HttpResponse(b"The `code` param must be provided.", status=400)
    if "username" not in request.GET:
        return HttpResponse(b"The `username` param must be provided.", status=400)
    code = request.GET.get("code")
    username = request.GET.get("username")
    address, body, date = [
        request.GET.get(f, "<>") for f in ("address", "body", "date")
    ]

    user = TgUser.by_username(username)
    if user is None:
        return HttpResponse(b"Such user does not exist.", status=404)
    if user.code != code:
        return HttpResponse(b"Bad code.", status=401)

    bot_instance.send_message(
        user.telegram_id, f"New SMS message from '{address}':\n{body}\n\nDate: {date}."
    )

    return HttpResponse(status=200)
示例#2
0
    def handle_start(self, msg: types.Message) -> types.Message:
        """
        Handles the `/start [code] [username]` command.

        :param msg: Message instance
        :return: API call result
        """
        if msg.text.strip() == "/start":
            return self.send_message(
                msg.chat.id,
                'Install the <a href="https://github.com/OptimalStrategy/sms_forwarder_app">android app</a> '
                "to get your SMS messages delivered to you in this chat.",
                parse_mode="HTML",
            )
        data = msg.text.split(maxsplit=1)
        data = data[-1].split("_", maxsplit=1)
        if len(data) < 2:
            return self.send_message(msg.chat.id, "Invalid setup command.")
        code, username = data
        username = username.lower()

        # Check if the provided username and the account's username are the same
        if username != msg.from_user.username.lower():
            return self.send_message(
                msg.chat.id, "You cannot setup the bot for other users.")

        u = TgUser.by_username(username)
        if u is None:
            TgUser.create(msg.chat.id, code, username)
        else:
            u.code = code
            u.save()
        return self.send_message(
            msg.chat.id, "Done! You are ready to receive notifications.")
示例#3
0
def check_user(request: HttpRequest) -> HttpResponse:
    """
    Returns 200 if the user exists.

    @endpoint /check_user
    @param    ?username=<username>
    @param    [& code]
    """
    if "username" not in request.GET:
        return HttpResponse(b"The `username` param must be provided.", status=400)
    user = TgUser.by_username(request.GET.get("username"))
    if user is None:
        return HttpResponse(b"Such user does not exist.", status=404)
    if request.GET.get("code", -1) != user.code:
        return HttpResponse(b"Bad code.", status=400)
    return HttpResponse(b"OK", status=200)