Package: easydingbot
easydingbotEasydingbot is a package to make dingbot easily to use.InstallationYou can install easydingbot bypip$pipinstalleasydingbotor$ pip install git+https://github.com/seniverse/easydingbot@mainConfigurationYou must inject your dingbot'swebhookandsecret-codeinto configuration before using easydingbot.Please seedingbot's documentationto learn how to get webhook and secret-code.After that, you can inject them byeasydingbot add-dingbotcommand:$easydingbotadd-dingbot
It'sfirsttimetosetdingbot,wewilluse"default"asthefirstdingbotid.
Pleaseinputthewebhookstring("q"toquit)>
Pleaseinputthesecretstring("q"toquit)>Then you can list all dingbots already in configuration byeasydingbot ls-dingbotcommand:$easydingbotls-dingbot
Thereare1dingbotsinconfigasfollow:*defaultIf you want to check whether this configuration can work well, you can useeasydingbot touchcommand to test for once:$ easydingbot touch
Dingbot of default's status is normal.The command above is equal toeasydingbot touch default.If you want another's dingbot config, you can pass a new name(dingbot id) toeasydingbot add-dingbot:$easydingbotadd-dingbotanother
Pleaseinputthewebhookstring("q"toquit)>
Pleaseinputthesecretstring("q"toquit)>Then you will have two dingbot in your configuration.$ easydingbot ls-dingbot
There are 2 dingbots in config as follow:
  * another
  * defaultAnd you can assign their ids to touch.$ easydingbot touch another
Dingbot of another status is normal.If you find your touched result is abnormal and suspect you inputed wrong webhook or secret-code, you can useeasydingbot add-dingbotagain, type the same dingbot id to overwrite it.If you want to remove one of dingbot, you can useeasydingbot rm-dingbotcommand:$easydingbotrm-dingbot
Thereare2dingbotsinconfigasfollow:*another*default
Pleasechooseoneofabovetoremove("q"toquit)>another
RemovedanotherfromconfigIn addition, you can pass their ids to remove directly:$ easydingbot rm-dingbot another default
Removed another from config
Removed default from configUsageEasydingbot's purpose is to make dingbot more easily to use, therefore it has only two concise function for now:informandfeedback.Let's start with theinformfunction. It is the most basic function to send specific message to specific dingbot.informhas three arguments ofdingbot_id,titleandtext. You can assigndingbot_idto choose one of dingbots to send,titleandtextcan decide the content you are sending.>>>fromeasydingbotimportinform# to "default" dingbot with default title and text>>>inform()# to "default" dingbot with title of "my inform" and text of "something">>>inform(title='my inform',text='something')# to "another" dingbot with default title and text>>>inform(dingbot_id='another')# to "another" dingbot with title of "my inform" and text of "something">>>inform(dingbot_id='another',title='my inform',text='something')If your configuration is correct, then the messages would be sent to your dingtalk groups.After that, let's see thefeedbackdecorator. This decorator is design for some long-period manual task's feedback.For example, you run a program withnohup python ... &, you estimated this program would run for a long time and got away to do something else. No one can tell you the program's running status before you rechecking its log, in other words, no feedback. Think that you find it crashed at 6 hours ago when you recheck the log, maybe you are going mad. Or you find it already finished at 6 hours ago, you wasted 6 hours for subsequent processing. So you need someone to tell you the program's status regardless of finish or crash,feedbackcan do this.feedbackdecorator has only two arguments ofdingbot_idandtitle, you can assigndingbot_idto choose which dingbot to send, and assigntitleto identify the task name.>>>importtime>>>fromeasydingbotimportfeedback>>>@feedback()...deflong_time_succeed():...time.sleep(3)...>>>long_time_succeed()('{"errcode":0,"errmsg":"ok"}','{"errcode":0,"errmsg":"ok"}')For this example, it will send 2 messages like:TASK NAMETIME: 2020-11-26T15:07:20.738476+08:00STATUS: START RUNNINGTASK NAMETIME: 2020-11-26T15:07:22.861785+08:00STATUS: FINISHEDELAPSED TIME: 0:00:02.123238Thelong_time_succeed's return value will be modified byfeedbackto the message status,('{"errcode":0,"errmsg":"ok"}', '{"errcode":0,"errmsg":"ok"}')means the two messages have been sent to dingtalk's host. Since this feature, you should not addfeedbackto any return-value-matters function.
For a task going to crash.>>>@feedback(dingbot_id='another',title='ANOTHER TASK')...deflong_time_failed():...time.sleep(3)...1/0...>>>long_time_failed()('{"errcode":0,"errmsg":"ok"}','{"errcode":0,"errmsg":"ok"}','ZeroDivisionError: division by zero')For crashed task, it will return another value of the error's type.It will send 2 messages like:ANOTHER TASKTIME: 2020-11-26T15:07:22.962419+08:00STATUS: START RUNNINGANOTHER TASKTIME: 2020-11-26T15:07:25.056812+08:00STATUS: CRASHEDTRACKBACK:Traceback (most recent call last):
 File "/Users/clarmylee/SNV/LAB/easy-dingbot/easydingbot/main.py", line 116, in wrapper
   result = function(*args, **kwargs)
 File "/Users/clarmylee/SNV/LAB/easy-dingbot/tests/test_feedback.py", line 50, in some_task_failed
   1 / 0
ZeroDivisionError: division by zeroIn addition, you can pass custom arguments to show in result text, for example:>>>@feedback(title='CUSTOM',AUTHOR='CLARMY',PRIORITY=5)...deflong_time_succeed():...print('long time')...>>>long_time_succeed()longtime('{"errcode":0,"errmsg":"ok"}','{"errcode":0,"errmsg":"ok"}')The result:CUSTOMAUTHOR: CLARMYPRIORITY: 5TIME: 2020-11-28T03:18:49.143275+08:00STATUS: START RUNNINGCUSTOMAUTHOR: CLARMYPRIORITY: 5TIME: 2020-11-28T03:18:49.293935+08:00STATUS: FINISHEDELAPSED TIME: 0:00:00.150647