WeWorkBot
1.0.0
Robot yang dibangun untuk aplikasi mandiri perusahaan WeChat dapat mewujudkan balasan pasif dan pengiriman aktif berbagai jenis pesan.
Sekarang didukung:
text ), gambar ( image ), suara ( voice ), video ( video ), lokasi ( location ) dan tautan ( link );text , image , voice , video , dan news ;text , image , voice , video , markdown , teks grafis ( mpnews ), teks grafis ( news ), kartu teks ( textcard ), file ( file ) dan jenis pesan lainnya; Tidak banyak perpustakaan yang bergantung pada, silakan merujuk ke requirements.txt .
Saat ini mendukung Python >= 3.9 , sisa versi tidak diuji.
Perpustakaan Python sekarang dirilis dan dapat diinstal melalui pip install wwbot .
Kode sampel berikut menunjukkan penggunaan sederhana WeWorkBot . Contoh ini menunjukkan cara membalas pesan jenis teks ( text ):
from flask import Flask
from wwbot import WWBot
from wwbot . msg import Message , TextMessage
# 注册一个文本消息的事件监听
@ WWBot . on ( WWBot . TEXT )
def text_handler ( msg : TextMessage ) -> Message :
'''
msg参数代表接收到的消息被解析后的实例
'''
# 从消息中提取消息内容字段
# 关于接收到的消息格式具体定义,参考 https://developer.work.weixin.qq.com/document/path/90239
msg_content : str = msg . content
# 作为示例,直接使用接收到的消息作为回复,相当于一个 echo bot
return TextMessage ( msg . from_username , msg . to_username , msg . agent_id , msg_content )
# WeWorkBot运行在Flask框架之上
app : Flask = Flask ( 'WWBot' )
# 企业ID
corp_id : str = 'corp_id'
# 企业自建应用的secret 可以创建自建应用后在应用详情页面查看
corp_secret : str = 'corp_secret'
# 自建应用启用API接收消息时,配置的“Token”参数
token : str = 'token'
# 自建应用启用API接收消息时,配置的“EncodingAESKey”参数
aes_key : bytes = base64 . b64decode ( 'aes_key' )
# 接收消息回调时的url path部分
callback_path : str = '/wwbot'
# 配置机器人
WWBot . config ( app , corp_id , corp_secret , token , aes_key , callback_path = callback_path )
if __name__ == '__main__' :
app . run ( '0.0.0.0' , 31221 ) Untuk contoh lebih lengkap, silakan merujuk ke exampls/echo_bot.py .
Kode ini diperbarui sesuka Anda, terutama tergantung pada apakah ada ruang atau tidak. Biasanya akan sering diperbarui pada akhir pekan.