コード例 #1
0
ファイル: FollowEvent.py プロジェクト: GoldGliders/Alexandria
def followevent(event=None):
    user_doc = record.user  # copy the record to save user information
    userid = event.source.user_id if event else "testid"
    hashed_userid = sha256(userid.encode()).hexdigest()

    user_doc["userid"] = hashed_userid
    user_doc["context"] = ""
    user_doc["options"] = {
        "calil": True,
        "google": False,
        "honto": False,
        "amazon": True,
        "rakuten": False,
        "yodobashi": False,
        "yahoo": False,
        "mercari": True,
        "rakuma": False,
        "paypayfleamarket": False
    }

    status = ""
    try:
        logger.debug(user_doc)
        if db.user.find(hashed_userid):
            raise DuplicateUserError

        db.user.set(hashed_userid, user_doc)
        logger.info("Successed in registaring new follower!")
        status = """登録ありがとうございます。\nメニューにて"Help"を参照してください。"""

    except DuplicateUserError:
        logger.info("This user is already registared.")
        status = "This user is already registared"

    except Exception as e:
        logger.info("Faild to registaring new follower!")
        logger.info(e)
        status = "Fail"

    finally:
        if event:
            line_bot_api.reply_message(
                event.reply_token,
                messages=TextSendMessage(text=status)
            )

    return status
コード例 #2
0
def add_library(event=None, libinfo=None):
    try:
        status = add.favolib(event, libinfo)

    except InvalidName:
        logger.info("This user is not registared.")
        status = "This user is not registared"

    except DuplicateLibraryError:
        logger.info("This library is already registared.")
        status = "This library is already registared"

    except Exception as e:
        logger.info(e)
        status = "fail"

    finally:
        if event:
            line_bot_api.reply_message(event.reply_token,
                                       messages=TextSendMessage(text=status))
コード例 #3
0
def chat(event=None):
    # user_doc = record.user # copy the record to save user information
    # userid = event.source.user_id if event else "testid"
    # hashed_userid = sha256(userid.encode()).hexdigest()
    text = event.message.text if event else "some text"

    status = ""
    try:
        cmd = text.split(" ")
        if event.reply_token == "00000000000000000000000000000000":
            event = None
            return 200

        elif cmd[0] == "help":
            status = HELP

        elif cmd[0].replace("-", "").isdigit() and len(cmd[0].replace(
                "-", "")) == 13:
            isbn2message(event, cmd[0].replace("-", ""))
            event = None

        else:
            status = "すみません、よくわかりません。"

        logger.info("Successed chatting!")

    except DuplicateLibraryError:
        status = "This library is already registared"

    except Exception as e:
        logger.info("Faild to registaring new follower!")
        logger.info(e)
        status = "Fail"

    finally:
        if event:
            line_bot_api.reply_message(event.reply_token,
                                       messages=TextSendMessage(text=status))

    return status
コード例 #4
0
def add_bookmark(event=None):
    try:
        status = add.bookmark(event)

    except DuplicateUserError:
        logger.info("This user is already registared.")
        status = "This user is already registared"

    except DuplicateBookError:
        logger.info("This book is already registared.")
        status = "This book is already registared"

    except UserNotFound:
        logger.info("Users are not found.")
        status = "Users are not found."

    except Exception as e:
        logger.info(e)
        status = "fail"

    finally:
        if event:
            line_bot_api.reply_message(event.reply_token,
                                       messages=TextSendMessage(text=status))
コード例 #5
0
def isbn2message(event, isbn):
    """
    Create from isbn to message and send it.

    Parameters
    ----------
    event: LINE event
        LINE event
    isbn: str
        The isbn which a user input.

    Returns
    -------
    None

    Raises
    ------
    None
    """

    userid = event.source.user_id if event else "testid"
    hashed_userid = sha256(userid.encode()).hexdigest()
    favolib = db.user.find(hashed_userid)["favolib"]
    logger.debug(favolib)
    bookstatus = []

    try:
        bookdoc = call_api.openbd(isbn)
        if bookdoc:
            flex_message = send.bookmeta(bookdoc, hashed_userid)
            add.history(event, bookdoc)

        else:
            bookdoc, flex_message = send.compact_bookmeta(isbn)
            add.history(event, bookdoc)

        line_bot_api.push_message(event.source.user_id, messages=flex_message)

        for library in favolib:
            try:
                bookstatus.append(
                    (call_api.calil(isbn,
                                    library["systemid"]), library["systemid"]))
            except BookNotFound:
                pass

        if bookstatus:
            # When you cannot get book meta information from google books api, you pass NoneType to this function.
            flex_message = send.bookstatus(bookstatus)
            line_bot_api.reply_message(event.reply_token,
                                       messages=flex_message)
            event = None

        else:
            raise BookNotFound

    except MetadataNotFound:
        pass

    except BookNotFound:
        logger.info("book not found")
        status = "book not found"

    except Exception as e:
        logger.info(e)
        status = "Fail"

    finally:
        # A message is already sent, event is None.
        if event:
            flexcontent = flexbox.notfound
            flexcontent["body"]["contents"][0]["text"] = status
            line_bot_api.reply_message(event.reply_token,
                                       messages=FlexSendMessage(
                                           alt_text=status,
                                           contents=flexcontent))