From fbae56386443e9d17c7b89e3c368a5d7e8e86fc1 Mon Sep 17 00:00:00 2001 From: congxu Date: Wed, 12 Apr 2023 20:10:46 +0800 Subject: [PATCH 1/3] Add files via upload --- plugins/plugin_getnews/__init__.py | 1 + plugins/plugin_getnews/getnews.py | 63 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 plugins/plugin_getnews/__init__.py create mode 100644 plugins/plugin_getnews/getnews.py diff --git a/plugins/plugin_getnews/__init__.py b/plugins/plugin_getnews/__init__.py new file mode 100644 index 000000000..bb629f138 --- /dev/null +++ b/plugins/plugin_getnews/__init__.py @@ -0,0 +1 @@ +from .getnews import * \ No newline at end of file diff --git a/plugins/plugin_getnews/getnews.py b/plugins/plugin_getnews/getnews.py new file mode 100644 index 000000000..814d32caf --- /dev/null +++ b/plugins/plugin_getnews/getnews.py @@ -0,0 +1,63 @@ +# encoding:utf-8 + +from bridge.context import ContextType +from bridge.reply import Reply, ReplyType +from channel.chat_message import ChatMessage +import plugins +from plugins import * +from common.log import logger +import requests +import json + + +@plugins.register(name="getnews", desire_priority=-1, hidden=True, desc="A simple plugin that says getnews", version="0.1", author="congxu") +class getnews(Plugin): + def __init__(self): + super().__init__() + self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context + self.url = "https://v2.alapi.cn/api/zaobao" + self.headers = {'Content-Type': "application/x-www-form-urlencoded"} + self.getnews_api_token = "UDuxUGXTKAlCJ3qt" + self.payload = "token="+getnews_api_token+"&format=json" + logger.info("[getnews] inited") + + def on_handle_context(self, e_context: EventContext): + + if e_context['context'].type != ContextType.TEXT: + return + + content = e_context['context'].content + logger.debug("[getnews] on_handle_context. content: %s" % content) + if content == "news": + reply = Reply() + reply.type = ReplyType.TEXT + #获取新闻 + req = requests.request("POST", url, data=payload, headers=headers) + news_json = json.loads(req.text) + news_date = news_json["data"]["date"] + news_reasult = '\n'.join(news_json["data"]["news"]) + + msg:ChatMessage = e_context['context']['msg'] + if e_context['context']['isgroup']: + reply.content = f"早上好, " + news_date +"\n" + response + else: + reply.content = f"早上好, " + news_date +"\n" + response + e_context['reply'] = reply + e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑 + + # if content == "Hi": + # reply = Reply() + # reply.type = ReplyType.TEXT + # reply.content = "Hi" + # e_context['reply'] = reply + # e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑,一般会覆写reply + + # if content == "End": + # # 如果是文本消息"End",将请求转换成"IMAGE_CREATE",并将content设置为"The World" + # e_context['context'].type = ContextType.IMAGE_CREATE + # content = "The World" + # e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑 + + def get_help_text(self, **kwargs): + help_text = "输入getnews,今天新闻\n" + return help_text From 62595dc37f67c4cf990bd03da211f4e56394a8da Mon Sep 17 00:00:00 2001 From: congxu Date: Wed, 12 Apr 2023 20:19:27 +0800 Subject: [PATCH 2/3] Update getnews.py --- plugins/plugin_getnews/getnews.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/plugin_getnews/getnews.py b/plugins/plugin_getnews/getnews.py index 814d32caf..f121ec69e 100644 --- a/plugins/plugin_getnews/getnews.py +++ b/plugins/plugin_getnews/getnews.py @@ -10,7 +10,7 @@ import json -@plugins.register(name="getnews", desire_priority=-1, hidden=True, desc="A simple plugin that says getnews", version="0.1", author="congxu") +@plugins.register(name="getnews", desire_priority=-1, hidden=True, desc="A simple plugin that get news", version="0.1", author="congxu") class getnews(Plugin): def __init__(self): super().__init__() @@ -18,7 +18,7 @@ def __init__(self): self.url = "https://v2.alapi.cn/api/zaobao" self.headers = {'Content-Type': "application/x-www-form-urlencoded"} self.getnews_api_token = "UDuxUGXTKAlCJ3qt" - self.payload = "token="+getnews_api_token+"&format=json" + self.payload = "token="+self.getnews_api_token+"&format=json" logger.info("[getnews] inited") def on_handle_context(self, e_context: EventContext): From 62324b0cd2efa47ca249faffc4d173edf9557eb0 Mon Sep 17 00:00:00 2001 From: congxu Date: Wed, 12 Apr 2023 20:20:31 +0800 Subject: [PATCH 3/3] Update source.json --- plugins/source.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/source.json b/plugins/source.json index 98d141aa5..8272f3556 100644 --- a/plugins/source.json +++ b/plugins/source.json @@ -10,7 +10,7 @@ } , "getnews": { - "url": "https://github.com/congxuma/plugin_getnews", + "url": "https://github.com/congxuma/plugin_getnews.get", "desc": "测试获取新闻" } }