Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

基于 Itchat 为 Python Wechaty 实现社区首个 Python 语言的本地 Puppet #75

Open
huan opened this issue Mar 30, 2021 · 17 comments

Comments

@huan
Copy link
Member

huan commented Mar 30, 2021

开源软件供应链点亮计划

“开源软件供应链点亮计划-暑期2021”(以下简称 暑期2021)是由中科院软件所与 openEuler 社区共同举办的一项面向高校学生的暑期活动,旨在鼓励在校学生积极参与开源软件的开发维护,促进国内优秀开源软件社区的蓬勃发展。

根据项目的难易程度和完成情况,参与者还可获取“开源软件供应链点亮计划-暑期2021”活动奖金和奖杯。

官网:https://summer.iscas.ac.cn

Wechaty

Wechaty 是一个开源聊天机器人框架SDK,具有高度封装、高可用的特性,支持NodeJs, Python, Go 和Java 等多语言版本。在过去的5年中,服务了数万名开发者,收获了 Github 的 9600 Star。同时配置了完整的DevOps体系并持续按照Apache 的方式管理技术社区。

项目名称

基于 ItchatPython Wechaty 实现社区首个 Python 语言的本地 Puppet

背景介绍

Wechaty 社区目前已经支持微信、Whatsapp、企业微信、飞书等常见流行即时通讯工具,并且能够通过多语言 SDK (比如 Python Wechaty) 进行调用。

Python Wechaty 目前通过 Puppet Service 模块,基于 gRPC 与 TypeScript 语言的服务器进行通讯,实现对 Python 语言的支持。

我们在本次 Summer 2021 的项目中,希望可以开发一个基于 Python 语言的 Wechaty Puppet Provider 实现(模块名:wechaty-puppet-itchat),通过封装 Itchat ,使其成为 Python Wechaty 可以使用的社区生态模块。

需求介绍

使用 https://github.com/wechaty/python-wechaty-puppet-service 项目作为模版,将核心代码文件 https://github.com/wechaty/python-wechaty-puppet-service/blob/master/src/wechaty_puppet_service/puppet.py 中的 gRPC 调用,全部替换(封装)为 Itchat 模块的调用。

在初期开发中,能够实现文本消息的接收和发送,即可完成原型验证 POC 。

除了 Python 代码之外,还可以参考以下两个链接:

  1. TypeScript Puppet Official Documentation: https://wechaty.github.io/wechaty-puppet/typedoc/classes/puppet.html
  2. Wechaty Puppet Specification: https://wechaty.js.org/docs/specs/puppet

项目难度

中(奖金:9000元)

导师联系方式

吴京京:Python Wechaty creator, Wechaty committer, Microsoft Research Asia Intern

项目产出目标

  1. 每日代码 commit
  2. 每周提交一份 report (回复本 issue)
  3. 每两周一次在线会议
  4. 发布 Git Repo python-wechaty-puppet-itchat
  5. 可以通过 Python Wechaty 加载 wechaty-puppet-itchat 模块,并通过 itchat 底层,实现文本消息的收发功能
  6. 提供一个 examples/ding-dong-bot.py ,完成“接收到文字消息ding时,自动回复消息dong"的功能
  7. 配置 GitHub Actions 实现自动化测试* (可选)

项目技术栈

  1. Python programming language
  2. Git
  3. Pypi / pip tools

Hello students!

We want Wechaty to be the universal instant messaging SDK on all IM platforms for chatbot makers to build their great conversational user interface, so while these are our ideas of what we think would make good projects for the summer, we're eager to hear your ideas and proposals as well.

Try us out and get to know the Wechaty code with examples!

What we want to do

Implement Itchat Puppet (ItChat) for Python Wechaty. The Puppet abstraction allows Wechaty to provide a consisting API on all IM platforms for build a chatbot. We have lots of puppet providers in TypeScript already, and we want to support our first Python Wechaty Puppet so that our Python developers can run their chatbots out-of-the-box with the Web protocol of WeChat.

  • Required skills: Python programming language, ItChat.
  • Likely mentors: @huan.

Useful Links

@huan huan changed the title Build a Python Puppet for Itchat 基于 Itchat 为 Python Wechaty 实现社区首个 Python 语言的本地 Puppet Mar 31, 2021
@jingdong00
Copy link

参与计划:
1.阅读gRPC调用模块,熟悉代码功能和需要的修改
2.参考wechaty puppet文档等,确定封装接口
3. 和mentor确认接口之后开始初步实现各个功能

@lyleshaw
Copy link
Member

This is my proposal for this project: Click here
It has been submitted to the iscas system~

@huan
Copy link
Member Author

huan commented Jun 12, 2021

@jingdong00 @lyleshaw Welcome!

Please notice that, in this project, we will need to use Itchat APIs to replace all gRPC APIs because this project will build a Puppet Provider (running locally) instead a Puppet Service (running remotely).

@lyleshaw
Copy link
Member

lyleshaw commented Jul 4, 2021

[基于 Itchat 为 Python Wechaty 实现社区首个 Python 语言的本地 Puppet] 进度报告

日期:2021-07-04

本周完成的成果

  1. 完成了puppet-itchat的初始化
  2. 研究了itchat获取二维码的方式

本周遇到的困难和需要配合的地方

  1. 对Wechaty/Puppet之间的通讯不够了解

下周的开发计划和预计成果

  1. 预计完成二维码接入部分
  2. 预计完成登录登出部分

其他需要说明和同步的事项

暂无

@lyleshaw
Copy link
Member

[基于 Itchat 为 Python Wechaty 实现社区首个 Python 语言的本地 Puppet] 进度报告

日期:2021-07-12

本周完成的成果

  1. 理解了puppet的event

本周遇到的困难和需要配合的地方

  1. 需要理一下如何把itchat的各种事件【接收消息、扫码、登录】转换成puppet的event。【之前由于使用gRPC,所以可以通过if/elif来根据状态来发送event,但是itchat的一系列操作可能更接近于一次性的调用而非长连接】

下周的开发计划和预计成果

  1. 预计完成二维码接入部分
  2. 预计完成登录登出部分

其他需要说明和同步的事项

该周由于上课和准备来京,所以进度不多...

@lyleshaw
Copy link
Member

lyleshaw commented Jul 18, 2021

[基于 Itchat 为 Python Wechaty 实现社区首个 Python 语言的本地 Puppet] 进度报告

日期:2021-07-18

本周完成的成果

  1. puppet-itchat可以登录啦;
  2. 对itchat的登录和扫码等部分有了更深入的理解;
  3. 对msg部分的处理方法有了一个理解

本周遇到的困难和需要配合的地方

均已与mentor沟通解决

  1. scan模块
payload = EventScanPayload(
    status=ScanStatus.Waiting,
    qrcode=uuid,
    data=payload_data.get('data', None))
self._event_stream.emit('scan', payload)

不理解qrcode和data的具体含义
2. wx_id部分
login_user_id在web中似乎没有找到
3. msg部分
start_receiving->MessageQueue->msg_register 流程不熟悉

下周的开发计划和预计成果

  1. 预计完善扫码状态部分
  2. 预计初步完成消息收取部分

其他需要说明和同步的事项

该周由于实习第一周,熟悉工作,因而进度较慢。
下周应该有所改善

@huan
Copy link
Member Author

huan commented Jul 18, 2021

puppet-itchat可以登录啦;

Congratulation! Looking forward to using native Python Wechaty soon!

@lyleshaw
Copy link
Member

项目中期报告

项目信息

项目名称

基于 Itchat 为 Python Wechaty 实现社区首个 Python 语言的本地 Puppet

方案描述

L0阶段:
预期效果:熟悉 Itchat 的各项 API ;熟悉 Wechaty Puppet 的开发流程;创建 python-wechaty-puppet-itchat 的 repository 。
实施方案:阅读 Itchat 文档,了解 Wechaty Puppet。

L1阶段:
预期效果:完成 python-wechaty-puppet-itchat 的登录与初始化部分的实现;完成 ding-dong-bot 的实现。
实施方案:了解 python-wechaty-puppet-service 是如何通过 gRPC 与远程服务器通讯的,并结合 Itchat 的设计模式,对比二者异同,思考如何将 Itchat 的函数封装进 wechaty-puppet ,思考 wechaty-puppet 的哪些代码对于该项目而言是不必要的,并完成对 Itchat 中 auto_login 函数的封装。 在此基础上深入了解 Itchat msg_register 函数的实现方法,并在此基础上封装进 wechaty-puppet ,同时完成发送消息函数的封装。同时参考其他 wechaty-puppet 完成 ding-dong-bot.py 并使其能正常运行。

L2阶段:
预期效果:配置 GitHub Action 实现 python-wechaty-puppet-itchat 自动发布到 Pypi 并完成自动化测试。
实施方案:了解 Pypi 的发布流程,并在参考已有 GitHub Action 配置文件的基础上完成自动发布的流程,同时完成自动化测试。

L3阶段:
预期效果:使用 Itchat 的 API 为 python-wechaty-puppet-itchat 实现 User 和 Room 部分的功能。
实施方案:在该阶段,我计划深入了解 Itchat 关于 User 部分的 get_contact 等函数的实现,封装为 Wechaty 的 User 相关模块的功能。同时将 Itchat create_chatroom 等函数封装为 Wechaty Room 模块的功能。

L4阶段:
预期效果:为 python-wechaty-puppet-itchat 实现 Itchat 的其他功能。
实施方案:在该阶段,我计划将 Itchat 剩余的接口根据 Wechaty 的代码风格封装进 python-wechaty-puppet-itchat 。

*L5阶段:
预期效果:完善 python-wechaty 文档。
实施方案:在了解 python-wechaty 的基础上,我计划在 python-wechaty 原有文档的基础上进行完善和补充。

时间规划

第一阶段(7月-8月)

  • L0阶段开发 5天
  • L1阶段开发 10天
  • L2阶段开发 20天
  • 中期验收

第二阶段(8月-10月)

  • L3阶段开发 10天
  • L4阶段开发 15天
  • L5阶段开发 15天
  • 结项验收

项目进度

已完成工作

当前效果展示

demo

遇到的问题及解决方案

困难1:对 CI/CD 中的各项工具不熟悉,推代码时遇到困难。

解决方案:根据导师推荐的资料,学习 GitHub Action 、 MakeFiles 、 pre-commit 等工具的用法,并配置 GPG key 等。

困难2:在 Wechaty Puppet 中无法正常使用 Itchat 的函数。

解决方案:根据导师的建议,深入了解了 Python 协程,将 Itchat 函数全部改为异步函数后得以解决。

后续工作安排

0815 - 0820 : 解决当前关于 AsyncIOEventEmitter 与 Itchat 事件顺序混乱的问题;

0821 - 0831 : 完成 Itchat 中 create_chatroom 、 set_chatroom_name 等关于 User 和 Room 部分接口的封装;

0901 - 0915 : 完成单元测试和代码优化;

0916 - 0930 : 考虑实现 Wechaty 接口中存在,但 Itchat 中没有的几个接口;

1001 - 1025 : 时间精力充足的情况下,完成对 Itchat 的重构工作,并在重构代码上修改 python-wechaty-puppet-itchat 的代码

@wj-Mcat
Copy link
Contributor

wj-Mcat commented Aug 19, 2021

评审结果

  • 项目完成度:项目已完成中期要求,针对于ItChat底层代码进行重构于封装,并完成了最底层的puppet基础开发,完成情况进度正常。
  • 学生参与度:学生暑期完成进度良好,与我保持了一定频率上的沟通,并能够积极发现并解决问题。
  • 代码贡献量:目前已完成ItChat整体代码的改造以及底层puppet模块的封装,已有基础的自动回复的聊天功能。
  • 综合评价及建议:总体来说项目完成情况不错,按照既定的规划稳定实施,项目功能模块也在逐步的完成。存在的问题如下:
    • 1、ItChat 模块的封装还不够完善,需要深入重构
    • 2、尚未调整 CI 到通过测试,后续需要完善
    • 3、还未完成事件系统的对接
  • 最终评审结果:“通过”

@lyleshaw
Copy link
Member

lyleshaw commented Aug 26, 2021

由于审核,可能短期不可见

[blibli] 中期汇报视频:https://www.bilibili.com/video/BV1Ef4y1G7hE/
[blibli] 中期汇报Demo演示:https://www.bilibili.com/video/BV15P4y1p7JN/
[youtube] 中期汇报视频:https://youtu.be/gjg1EW30pOw
[youtube] 中期汇报Demo演示:https://youtu.be/hce3c4WZylo

@huan
Copy link
Member Author

huan commented Sep 28, 2021

I have a question: how did we dependence the ItChat from our code base?

It is good practice if we are dependencies it from our requirements.txt. if we are copying the code to our repo, we should change it back to use the pip module dependence before we release this project.

Which means that we should dependence the itchat pip module from our project, and send PRs to our upstream when there's anything we can contribute.

@wj-Mcat
Copy link
Contributor

wj-Mcat commented Sep 28, 2021

python-wechaty is a async-based package, but ItChat is sync-based package which will occur some call problems: sync-program will call async-progrom. It can't call it directly, and should make some changes. There are two ways:

This is the final solution that we have researched. So, is there any suggestion about this problem & solution ? @huan

@huan
Copy link
Member Author

huan commented Sep 29, 2021

From my understanding:

  1. our Python Wechaty is async, and
  2. ItChat is sync

So it should be no problem for us to call the Sync program from Async?

@wj-Mcat
Copy link
Contributor

wj-Mcat commented Sep 29, 2021

Yes, there is no problem for async call sync. But itchat callback event body will call our async-based method which is the core problem. This is the first problem in developing stage.

Buuuuuuuuut, It seems that we can add adapter in this event calling. This adapter will be the inner connector between itchat and python-wechaty. I have not found this method before. I think I will make test with it later.

@huan
Copy link
Member Author

huan commented Sep 29, 2021

Awesome! I believe we should add an adapter in this event handling, and then we will be able to dependence Itchat based on that.

@lyleshaw
Copy link
Member

结项报告模板

项目信息

  • 项目名称:基于 Itchat 为 Python Wechaty 实现社区首个 Python 语言的本地 Puppet
  • 方案描述:
    • L0阶段:
      • 预期效果:熟悉 Itchat 的各项 API ;熟悉 Wechaty Puppet 的开发流程;创建 python-wechaty-puppet-itchat 的 repository
      • 实施方案:阅读 Itchat 文档,了解 Wechaty Puppet
    • L1阶段:
      • 预期效果:完成 python-wechaty-puppet-itchat 的登录与初始化部分的实现;完成 ding-dong-bot 的实现
      • 实施方案:了解 python-wechaty-puppet-service 是如何通过 gRPC 与远程服务器通讯的,并结合 Itchat 的设计模式,对比二者异同,思考如何将 Itchat 的函数封装进 wechaty-puppet ,思考 wechaty-puppet 的哪些代码对于该项目而言是不必要的,并完成对 Itchat 中 auto_login 函数的封装 在此基础上深入了解 Itchat msg_register 函数的实现方法,并在此基础上封装进 wechaty-puppet ,同时完成发送消息函数的封装同时参考其他 wechaty-puppet 完成 ding-dong-bot.py 并使其能正常运行
    • L2阶段:
      • 预期效果:配置 GitHub Action 实现 python-wechaty-puppet-itchat 自动发布到 Pypi 并完成自动化测试
      • 实施方案:了解 Pypi 的发布流程,并在参考已有 GitHub Action 配置文件的基础上完成自动发布的流程,同时完成自动化测试
    • L3阶段:
      • 预期效果:使用 Itchat 的 API 为 python-wechaty-puppet-itchat 实现 User 和 Room 部分的功能
      • 实施方案:在该阶段,我计划深入了解 Itchat 关于 User 部分的 get_contact 等函数的实现,封装为 Wechaty 的 User 相关模块的功能同时将 Itchat create_chatroom 等函数封装为 Wechaty Room 模块的功能
    • L4阶段:
      • 预期效果:为 python-wechaty-puppet-itchat 实现 Itchat 的其他功能
      • 实施方案:在该阶段,我计划将 Itchat 剩余的接口根据 Wechaty 的代码风格封装进 python-wechaty-puppet-itchat
    • *L5阶段:
      • 预期效果:完善 python-wechaty 文档
      • 实施方案:在了解 python-wechaty 的基础上,我计划在 python-wechaty 原有文档的基础上进行完善和补充
  • 时间规划:
    • 第一阶段(7月-8月)
      • L0阶段开发 5天
      • L1阶段开发 10天
      • L2阶段开发 20天
      • 中期验收
    • 第二阶段(8月-10月)
      • L3阶段开发 10天
      • L4阶段开发 15天
      • L5阶段开发 15天
      • 结项验收

项目总结

  • 项目产出:
  • 方案进度:
  • 遇到的问题及解决方案:
    • 困难1:对 CI/CD 中的各项工具不熟悉,推代码时遇到困难
    • 解决方案:根据导师推荐的资料,学习 GitHub Action 、 MakeFiles 、 pre-commit 等工具的用法,并配置 GPG key 等
    • 困难2:在 Wechaty Puppet 中无法正常使用 Itchat 的函数
    • 解决方案:根据导师的建议,深入了解了 Python 协程,将 Itchat 函数全部改为异步函数后得以解决
    • 困难3:在使用 AsyncIOEventEmitter 时发现事件无法 Emit 到 Wechaty 层
    • 解决方案:根据导师指导,添加 await asyncio.sleep() 后解决
  • 项目完成质量:
    • 已完成全部预定进度,Python Wechaty Puppet Itchat 已可以正常使用,且代码已通过 CI 检查,符合社区对代码规范的要求
    • 不足之处:在于由于时间有限,未能对 Itchat 的代码进行重构,该项任务将会在后续结项后继续完成
    • 综合以上两点,个人对项目完成质量评价:良好
  • 与导师沟通及反馈情况:
    • 与导师保持积极的沟通,能及时反馈疑问及问题
    • 反馈的问题会及时得到导师的回复,并且导师会通过讨论引导我独立解决

后续安排

  • Python Wechaty Puppet Itchat
    • 在结项后,我仍计划持续维护这一 Puppet,并计划将 Itchat 的代码进行重构
  • Python Wechaty
    • 我计划之后也持续参与到 Python Wechaty 生态的建设中去 (包括但不限于:为 Python Wechaty 贡献代码、为 python-wechaty-plugin-contrib 贡献 Plugin 等)
  • Wechaty
    • 我还计划于年末为 Wechaty 社区提供一个基于 CQHTTP 的 QQ Puppet wechaty-puppet-cq

总结

在本次开源之夏活动中,很荣幸中选 基于 Itchat 为 Python Wechaty 实现社区首个 Python 语言的本地 Puppet 项目。

通过对 Puppet Itchat 的开发,我收获的不仅仅是技术与代码规范上的提升,更是体会到了开源的魅力所在。

而该项目的完成,首先需要感谢 京京 老师,没有他对我的帮助我可能无法在这么短的时间内解决所遇到的问题,同时他解决问题的思路也带给我很大的提升。同时也感谢 卓桓佳芮 等老师,他们在我开发时提出了许多宝贵的建议且提供了很多帮助。

最终还是要感谢 Wechaty 社区与 开源之夏主办方,前者让我在这里认识到了许多可敬的老师与可爱的小伙伴,而倘若没有后者,我也不会有如此充实的假期。

@lyleshaw
Copy link
Member

lyleshaw commented Oct 7, 2021

由于审核,可能短期不可见

[blibli] 结项汇报视频:https://www.bilibili.com/video/BV1z34y1U7MR/
[blibli] 结项汇报Demo演示:https://www.bilibili.com/video/BV1sP4y187Vj/
[youtube] 结项汇报视频:https://youtu.be/SKhgnRaljzk
[youtube] 结项汇报Demo演示:https://youtu.be/fGjEd5O97lQ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants