wechaty-python实现机器人

一、目标:
针对于微信用户,最近想做一个机器人,想要以下功能
1.群内定时发送点餐消息(已完成)
2.微信朋友圈重要信息发布
3.个人自定义智能机器人
4.群内热点提醒;朋友圈每日热点;个人热点新闻
5.天气查询功能
6.讲段子功能
注:如果想做群聊机器人有两种方式
1.完成微信用户群发(企业微信外部群):建立企业微信并建立外部群(可对没有使用企业微信的用户发消息),如果你要使用wechaty请直接看二及以后的教程
2.完成企业微信内部成员群发(企业微信内部群):可对企业内人员发消息,不用wechaty机器人,方便快捷,详见api接口

二、调研及结果:
1.ItChat、wechat-wxpy等等大部分开源包都不行(由于使用web协议,微信已经封了且很容易封号),据说17年以前申请的微信且没有换过绑定的手机号的可以使用web协议,也就是ItChat,但不稳定。

2.使用wechaty,该项目使用ipad协议,稳定可用,不会被封号,但比ItChat复杂一些;且需要申请token(国内为“句宝客”公司提供)

3.申请token免费须知:
(1)新账户可有7天使用权限
(2)每发一篇文章到各个平台(简书、知乎、掘金等等),一篇文章可使用3个月;(联系微信号juzibot)
(3)贡献一个MVP(最小项目),联系微信Juzi.Bot(微信号juzibot),可获得永久免费资格

如果你要买付费的token:200每月
详见开源奖励计划:https://wechaty.js.org/docs/contributor-program/
token详细说明:https://github.com/juzibot/Welcome/wiki/Everything-about-Wechaty#1Token-%E7%9A%84%E5%8A%9F%E8%83%BD%E5%92%8C%E7%94%B3%E8%AF%B7
三、可用的项目:https://github.com/wechaty/python-wechaty
步骤:(必须要python3.7以上版本,我用的python3.7)
1.安装wechaty
pip install wechaty

2.导入申请的token
export WECHATY_PUPPET_HOSTIE_TOKEN='your-token'3.编写代码1,最基础功能:发送ding会返回给你一张图片
 

引用来自https://zhuanlan.zhihu.com/p/268885550
import asyncio
from wechaty import Wechaty, Message

class MyBot(Wechaty):
    async def on_message(self, msg: Message):
        talker = msg.talker()
        await talker.ready()
        if msg.text() == "ding":
            await talker.say('dong')
        elif msg.text() == 'image':
            file_box = FileBox.from_url(
                'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/'
                'u=1116676390,2305043183&fm=26&gp=0.jpg',
                name='ding-dong.jpg')
            await talker.say(file_box)

async def main():
    bot = MyBot()
    await bot.start()

asyncio.run(main())

4.编写代码2:完成定时群发功能


#encoding = utf-8                                                                                                                                                                                            
from wechaty import Wechaty
from wechaty.plugin import WechatyPlugin
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import asyncio
import datetime
#from wechaty import get_room_id

class DailyPlugin(WechatyPlugin):
  @property
  def name(self) -> str:
    return 'dayily'

  async def tick(self):
    rooms = await self.bot.Room.find_all()
    for currRoom in rooms:
        print(f'room pay load topic: {currRoom.payload.topic}, topic: {currRoom.topic}, currRoom.room_id: {currRoom.room_id}')

        topic =  currRoom.payload.topic
        if(topic.startswith("ding") or topic.startswith("曼玲-食神券坊外卖")):
            room = currRoom
            await room.say(f'您好,我是食神券坊智能机器人,今天正式上线! -> {datetime.datetime.now()}')

  async def init_plugin(self, wechaty: Wechaty):
    await super().init_plugin(wechaty)
    scheduler = AsyncIOScheduler()
    scheduler.add_job(self.tick, 'cron', hour=9, minute=43, second=55)
    scheduler.start()

async def main():
  bot = Wechaty().use(DailyPlugin())
  await bot.start()

asyncio.run(main())


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值