DingTalk Stream Mode Jie Shao
Python SDK for DingTalk Stream Mode API, Compared with the webhook mode, it is easier to access the DingTalk chatbot
Ding Ding Zhi Chi Stream Mo Shi Jie Ru Shi Jian Tui Song , Ji Qi Ren Shou Xiao Xi Yi Ji Qia Pian Hui Diao ,Gai SDK Shi Xian Liao Stream Mo Shi . Xiang Bi Webhook Mo Shi ,Stream Mo Shi Ke Yi Geng Jian Dan De Jie Ru Ge Lei Shi Jian He Hui Diao .
Kuai Su Zhi Nan
- An Zhuang SDK
- Kai Fa Yi Ge Stream Ji Qi Ren
Yi Xia Shi Li Zhu Ce Yi Ge Jia Fa Chu Li Hui Diao ,Ke Yi Shi Xian Yi Ge Jia Fa Ji Qi Ren (Gei Ji Qi Ren Fa Song 1+1,Hui Fu 2)
import argparse
import logging
from dingtalk_stream import AckMessage
import dingtalk_stream
def setup_logger():
logger = logging.getLogger()
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter('%(asctime)s %(name)-8s %(levelname)-8s %(message)s [%(filename)s:%(lineno)d]'))
logger.addHandler(handler)
logger.setLevel(logging.INFO)
return logger
def define_options():
parser = argparse.ArgumentParser()
parser.add_argument(
'--client_id', dest='client_id', required=True,
help='app_key or suite_key from https://open-dev.digntalk.com'
)
parser.add_argument(
'--client_secret', dest='client_secret', required=True,
help='app_secret or suite_secret from https://open-dev.digntalk.com'
)
options = parser.parse_args()
return options
class CalcBotHandler(dingtalk_stream.ChatbotHandler):
def __init__(self, logger: logging.Logger = None):
super(dingtalk_stream.ChatbotHandler, self).__init__()
if logger:
self.logger = logger
async def process(self, callback: dingtalk_stream.CallbackMessage):
incoming_message = dingtalk_stream.ChatbotMessage.from_dict(callback.data)
expression = incoming_message.text.content.strip()
try:
result = eval(expression)
except Exception as e:
result = 'Error: %s' % e
self.logger.info('%s = %s' % (expression, result))
response = 'Q: %s\nA: %s' % (expression, result)
self.reply_text(response, incoming_message)
return AckMessage.STATUS_OK, 'OK'
def main():
logger = setup_logger()
options = define_options()
credential = dingtalk_stream.Credential(options.client_id, options.client_secret)
client = dingtalk_stream.DingTalkStreamClient(credential)
client.register_callback_handler(dingtalk_stream.chatbot.ChatbotMessage.TOPIC, CalcBotHandler(logger))
client.start_forever()
if __name__ == '__main__':
main()
Gao Jie Shi Yong Fang Fa
Yi Shang Shi Li Zhong ,Cai Yong client.start_forever() Lai Qi Dong Yi Ge asyncio De ioloop.
You De Shi Hou ,Ni Xu Yao Zai Yi You De ioloop Zhong Shi Yong Ding Ding Stream Mo Shi ,Bu Shi Yong start_forever Fang Fa .
Ci Shi ,Ke Yi Shi Yong client.start() Dai Ti client.start_forever(). Zhu Yi :Xu Yao Zai Wang Luo Yi Chang Hou Zhong Xin Qi Dong
await client.start()
except (asyncio.exceptions.CancelledError,
websockets.exceptions.ConnectionClosedError) as e:
... # Chu Li Wang Luo Duan Xian Yi Chang
Kai Fa Jiao Cheng
Zai Jiao Cheng Wen Dang Zhong ,Ni Ke Yi Zhao Dao Geng Duo Ding Ding Stream Mo Shi De Jiao Cheng Wen Dang He Shi Li Dai Ma .
Te Bie Shuo Ming
Yin Pin Xie Cuo Wu ,Cong Jiu Ban Ben Sheng Ji Dao v0.13.0 Shi Hou ,Xu Yao Jiang register_callback_hanlder Xiu Gai Wei register_callback_handler