我正在尝试将slackapi的V2封装到一个类中,这样就可以封装有关bot的信息。下面是他们的一个示例片段:
import slack
slack_token = os.environ["SLACK_API_TOKEN"]
rtmclient = slack.RTMClient(token=slack_token)
@slack.RTMClient.run_on(event='message')
def say_hello(**payload):
data = payload['data']
if 'Hello' in data['text']:
channel_id = data['channel']
thread_ts = data['ts']
user = data['user']
webclient = payload['web_client']
webclient.chat_postMessage(
channel=channel_id,
text="Hi <@{}>!".format(user),
thread_ts=thread_ts
)
rtmclient.start()
我在这里的理解是,这个say_hello
函数由于decorator被传递到slack对象中,所以如果我要将它包装到一个类中,那么这个函数实际上并不在我的类中。如何包装say_hello
函数,使其能够调用属于类实例的方法和引用属性?你知道吗
看看装饰师是怎么工作的!你知道吗
也许有一个更好的,规范的方法来实现你想要的,但这可以做到:
相关问题 更多 >
编程相关推荐