首页 > 解决方案 > 在 Rasa Actions 中调用自定义函数

问题描述

我在使用 rasa 开发聊天机器人时遇到问题。

我正在尝试在 rasa 操作文件中调用自定义函数。但我收到一条错误消息,提示“未定义名称‘areThereAnyErrors’”

这是我的动作课。我想从 run 方法调用 areThereAnyErrors 函数。有人可以帮忙解决这个问题吗?

类 ActionDayStatus(动作):

def areThereAnyErrors(procid):
    errormessagecursor = connection.cursor()
    errormessagecursor.execute(u"select  count(*) from MT_PROSS_MEAGE where pro_id = :procid and msg_T =    :messageT",{"procid": procid, "messageT": 'E'})
    counts = errormessagecursor.fetchone()
    errorCount = counts[0]
    print("error count is {}".format(errorCount))
    if errorCount == 0:
        return False
    else:
        return True

def name(self):
    return 'action_day_status'

def run(self, dispatcher, tracker, domain):
    import cx_Oracle
    import datetime
        # Connect as user "hr" with password "welcome" to the "oraclepdb" service running on this computer.
    conn_str = dbconnection
    connection = cx_Oracle.connect(conn_str)
    cursor = connection.cursor()
    dateIndicator = tracker.get_slot('requiredDate')
    delta = datetime.timedelta(days = 1)
    now = datetime.datetime.now()
    currentDate = (now - delta).strftime('%Y-%m-%d')
    print(currentDate)
    cursor = connection.cursor()

    cursor.execute(u"select  * from M_POCESS_FILE where CREATE_DATE >= TO_DATE(:createDate,'YYYY/MM/DD') fetch first 50 rows only",{"createDate":currentDate})

    all_files = cursor.fetchall()
    total_number_of_files = len(all_files)

    print("total_number_of_files are {}".format(total_number_of_files))

标签: chatbotrasa-nlurasa-core

解决方案


一位知识分子的回答:

https://realpython.com/instance-class-and-static-methods-demystified/决定是否需要静态方法或类方法或实例方法并适当地调用它。此外,当您在函数中使用连接时,它应该是成员变量或传递给方法您没有 self 作为参数,因此您可能打算将其作为静态方法 - 但您没有这样创建它


推荐阅读