You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python的skpy库开发Skype Bot时遭遇事件处理问题

解决Skype Bot重复回复自身消息的问题

你的代码核心问题是没有过滤Bot自身发送的消息,导致每次Bot发送回复后,这条消息又会触发NewMessage事件,进而重复执行回复逻辑,陷入无限循环。另外代码还存在两个小问题:一是未导入SkypeEventLoop类,二是依赖全局变量sk不够规范。

以下是修正后的代码:

from skpy import Skype, SkypeEventLoop  # 补充导入SkypeEventLoop

user = '***'
password = '***'
group_id = '***'

class MySkype(SkypeEventLoop):
    def onEvent(self, event):
        if event.type == 'NewMessage':
            # 过滤掉Bot自身发送的消息
            if event.userId != self.userId:
                message_content = event.raw['resource']['content']
                message_sender = event.raw['resource']['imdisplayname']
                # 使用self获取聊天对象,避免依赖全局变量
                ch = self.chats.chat(group_id)
                ch.sendMsg(f'The code is responding to {message_sender}.\n You sent this message: {message_content}')

sk = MySkype(user, password, autoAck=True)
sk.loop()

关键修改说明:

  • 补充导入SkypeEventLoop:原代码继承了该类但未导入,会导致运行报错;
  • 添加自身消息过滤:通过event.userId != self.userId判断消息是否来自其他用户,排除Bot自己发送的消息;
  • 使用self替代全局变量sk:在类方法中直接调用自身实例的属性,代码更健壮。

内容的提问来源于stack exchange,提问作者Seyed Kamyar Pakdaman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 12:35:24