gpt4 book ai didi

python - 在 Slack 中删除消息

转载 作者:太空宇宙 更新时间:2023-11-04 00:39:08 25 4
gpt4 key购买 nike

Sooo,我对编程还比较陌生,正在尝试学习如何使用 API。我想我会首先构建一个 Slack 机器人来进行审核,因为我经常使用 Slack。在大多数情况下,除了我尝试删除消息时,一切正常。 API 返回说它找不到消息,即使它在 channel 中(slack API 使用时间戳来定位所述消息)。时间戳匹配,但声明消息不存在。这是我的代码:

def __init__(self, token):
self.token = token
self.users = {}
self.channels = {}
self.slack = SlackClient(self.token)
self.as_user = True

def connect(self):
if self.slack.rtm_connect():
self.post_message('#test', "*AUTOMOD* _v0.1_")

while True:
# print(self.slack.rtm_read())
self.parse_data(self.slack.rtm_read())
time.sleep(1)

def parse_data(self, payload):
if payload:
if payload[0]['type'] == 'message':
print(("user: {} message: {} channel: {}").format(payload[0]['user'], payload[0]['text'], payload[0]['channel']))
self.handle_message(payload[0])

def handle_message(self, data):
# these users can post whatever they want.
WHITELISTED = ["U4DU2TS2F", "U3VSRJJD8", "U3WLZUTQE", "U3W1Q2ULT"]

# get userid
sent_from = (data['user'])

# ignore whitelisted
if sent_from in WHITELISTED:
return

# if message was sent from someone not in WHITELISTED, delete it
else:
print(("\n\ntimestamp of message: {}").format(data['ts']))
self.delete_message(data['channel'], data['ts'])
self.post_message(data['channel'], "```" + random.choice(dongers) + "```")


def delete_message(self, channel, timestamp):
print(("deleting message in channel '{}'...").format(channel))
print("timestamp check (just to make sure): ", timestamp)
deleted = self.slack.api_call("chat.delete",
channel=channel,
timestamp=timestamp,
as_user=self.as_user
)

if deleted.get('ok'):
print("\nsuccessfully deleted.\n")
else:
print(("\ncouldn't delete message: {}\n").format(deleted['error']))

输出

timestamp of message:                    1488822718.000040
deleting message in channel: 'G4DGYCW2X'
timestamp check (just to make sure...): 1488822718.000040

couldn't delete message: message_not_found

对可能发生的事情有什么想法吗?这是 chat.delete上下文方法。

编辑:

由于@pvg 推荐的“最小、完整和可验证的示例”,我已将项目的整个代码放在 gist 中。 .

最佳答案

一个问题可能是当 API 方法采用 ts 参数时,您似乎将 timestamp 参数传递给 chat.delete反而。 ( See docs )

关于python - 在 Slack 中删除消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42632628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com