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

封装此模块的通用模块 #27

Open
o1dcat opened this issue May 27, 2020 · 1 comment
Open

封装此模块的通用模块 #27

o1dcat opened this issue May 27, 2020 · 1 comment

Comments

@o1dcat
Copy link

o1dcat commented May 27, 2020

这个模块相当好,但还需要再封装成一个通用的模块。代码dingRobotDev.py如下:直接运行此文件python3 dingRobotDev.py 即可运行demo的text模式,发送如果失败,有三次机会发送。不足之处请指正。

#!/usr/env/python3
#encoding=utf-8

from enum import Enum
import time
from dingtalkchatbot.chatbot import DingtalkChatbot
from contextlib import contextmanager
from contextlib import closing

class sendkind(Enum):
text = 't'
link = 'l'
image = 'i'
markdown = 'm'
action_card = 'a'
feed_card = 'f'

class dingRobot(object):
def init(self,webhook='YOURWEBHOOK',secret=None,pc_slide=None,kind=sendkind.text,
msgargs={'msg':'我就是小丁,小丁就是我!','is_at_all':True},retry=3,flag=True):
self.webhook=webhook
self.secret=secret
self.pc_slide=pc_slide
self.kind=kind
print(self.kind)
print(self.kind.value)
print(self.kind.name)
self.msgargs=msgargs
self.retry=retry
self.flag=flag
self.kw = {'secret':self.secret,'pc_slide':self.pc_slide}
#self.xiaoding = None
self.xiaoding = DingtalkChatbot(self.webhook,**self.kw)

def pushMsg(self):
    kindsendfunc = 'self.xiaoding.send_%s'%self.kind.name
    yield eval('%s(**self.msgargs)'%kindsendfunc)

def demo(self):
    for t in range(self.retry):
        time.sleep(4**t)
        with closing(self.pushMsg()) as push:
            print(push)
            for i in push:
                print('in for',i)
        print(i)
        if i['errcode']==0 and i['errmsg']=='ok':
            break

if name == 'main':
dingRobot().demo()

@zhuifengshen
Copy link
Owner

1、从封装角度考虑,这样更通用,代码逻辑更简洁清晰;
2、不过,从调用者的角度考虑,发送消息时需要传递的参数比较多,特别是 ActionCard消息,这样使用起来就比较不方便,而且很容易传错;
综合以上考量后,采用了当前这种逐个方法调用方式。不过还是非常感谢你的补充,赞👍

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

No branches or pull requests

2 participants