nonebot / aiocqhttp

A Python SDK with async I/O for CQHTTP (OneBot).
https://aiocqhttp.nonebot.dev
MIT License
280 stars 39 forks source link

咨询一下,如何主动给指定QQ或者指定群发消息 #20

Closed qq20004604 closed 4 years ago

qq20004604 commented 5 years ago

我想实现定时(比如隔12个小时)给我自己的指定群发消息,类似闹钟。 请问如何实现?

stdrc commented 5 years ago

@qq20004604 可以用 APScheduler

qq20004604 commented 5 years ago

@qq20004604 可以用 APScheduler

可能我表达的不够清楚。 我指调用python-aiocqhttp的什么API,可以实现执行这个的时候,实现给群、或者指定用户发送消息。

sosyz commented 5 years ago

@qq20004604 没这功能吧,自己整个时钟调用发消息试试

qq20004604 commented 5 years ago

举例来说,await bot.send(context, '你好呀,下面一条是你刚刚发的:') 这个示例中,需要传 context,我觉得理论上这个就可以实现,但不清楚context怎么处理

stdrc commented 5 years ago

@qq20004604 把 context 存起来就可以了

qq20004604 commented 5 years ago

似乎并不是那么简单的事情。 举个例子来说,我写了以下代码

def start():
    print('start')
    bot.send(getGroupContxt(), message='小秘书已启动~你可以通过例如【#help】(不含中括号)来查看全部命令~现在时间是 %s' % datetime.now(),
             auto_escape=True)

我通过开多线程的方式,执行了这段代码,希望在bot刚启动的时候就对指定群发送信息。 getGroupContxt这个函数目前来看没有问题,有问题的是,会报以下错误。

RuntimeWarning: coroutine 'start' was never awaited
  self._do_submit_job(job, run_times)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

我的代码在这里 https://github.com/qq20004604/qq-robot/blob/master/src/app.py

stdrc commented 5 years ago

@qq20004604 那就用 bot.send_group_msg,具体参考一下 README

stdrc commented 5 years ago

@qq20004604 刚看清楚你用的是 aiocqhttp……

RuntimeWarning: coroutine 'start' was never awaited

这个问题是因为要 await bot.send(...)