Skip to content

Commit

Permalink
[Fix] 修复消息发送连接重置错误时,重试不生效问题
Browse files Browse the repository at this point in the history
[Fix] 修复消息发送连接重置错误时,重试不生效问题
  • Loading branch information
Cassius0924 authored Feb 3, 2024
2 parents b4fa773 + 4930a71 commit 763918b
Show file tree
Hide file tree
Showing 23 changed files with 131 additions and 99 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

</div>

<div align="center">

[![CI/CD](https://github.com/Cassius0924/WeChatter/actions/workflows/test.yml/badge.svg)](https://github.com/Cassius0924/WeChatter/actions/workflows/test.yml)
[![GitHub Release](https://img.shields.io/github/v/release/Cassius0924/WeChatter)](https://github.com/Cassius0924/WeChatter/releases)
[![GitHub License](https://img.shields.io/github/license/Cassius0924/WeChatter)](https://github.com/Cassius0924/WeChatter/blob/master/LICENSE)

</div>

## 介绍

基于 [wechatbot-webhook](https://github.com/danni-cool/wechatbot-webhook) 的微信机器人💬,支持 GPT 问答、热搜、天气预报、消息转发、Webhook提醒等功能。
Expand Down Expand Up @@ -123,7 +131,7 @@ python3 main.py

| 配置项 | 解释 | 备注 |
| --- | --- | --- |
| `wechatter_port` | WeChattter服务器的端口,接受消息的端口 | 默认为 `4000`,需和 `wxbotwebhook` Docker 的 `RECV_MSG_API` 参数的端口一致 |
| `wechatter_port` | WeChatter服务器的端口,接受消息的端口 | 默认为 `4000`,需和 `wxbotwebhook` Docker 的 `RECV_MSG_API` 参数的端口一致 |

### ⚙️ WxBotWebhook 配置

Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 项目启动文件
# WeChatter 启动文件
import uvicorn
from loguru import logger

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ bs4~=0.0.1
apscheduler==3.10.4
pre-commit==3.5.0
loguru==0.7.2
tenacity==8.2.3
8 changes: 6 additions & 2 deletions wechatter/commands/_commands/bili_hot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@


@command(
command="bili-hot", keys=["b站热搜", "bili-hot"], desc="获取b站热搜。", value=20
command="bili-hot",
keys=["b站热搜", "bili-hot"],
desc="获取b站热搜。",
)
def bili_hot_command_handler(to: SendTo, message: str = "") -> None:
try:
Expand All @@ -23,7 +25,9 @@ def bili_hot_command_handler(to: SendTo, message: str = "") -> None:


def get_bili_hot_str() -> str:
response = get_request_json(url="https://app.bilibili.com/x/v2/search/trending/ranking")
response = get_request_json(
url="https://app.bilibili.com/x/v2/search/trending/ranking"
)
hot_list = _extract_bili_hot_data(response)
return _generate_bili_hot_message(hot_list)

Expand Down
18 changes: 10 additions & 8 deletions wechatter/commands/_commands/copilot_gpt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
DEFAULT_CONVERSATIONS = [{"role": "system", "content": "你是一位乐于助人的助手"}]


@command(command="gpt35", keys=["gpt"], desc="使用GPT3.5进行对话。", value=30)
@command(
command="gpt35",
keys=["gpt"],
desc="使用GPT3.5进行对话。",
)
def gpt35_command_handler(to: SendTo, message: str = "") -> None:
_gptx("gpt-3.5-turbo", to, message)

Expand All @@ -26,7 +30,6 @@ def gpt35_command_handler(to: SendTo, message: str = "") -> None:
command="gpt35-chats",
keys=["gpt-chats", "gpt对话记录"],
desc="列出GPT3.5对话记录。",
value=31,
)
def gpt35_chats_command_handler(to: SendTo, message: str = "") -> None:
_gptx_chats("gpt-3.5-turbo", to, message)
Expand All @@ -36,7 +39,6 @@ def gpt35_chats_command_handler(to: SendTo, message: str = "") -> None:
command="gpt35-record",
keys=["gpt-record", "gpt记录"],
desc="获取GPT3.5对话记录。",
value=32,
)
def gpt35_record_command_handler(to: SendTo, message: str = "") -> None:
_gptx_record("gpt-3.5-turbo", to, message)
Expand All @@ -46,13 +48,16 @@ def gpt35_record_command_handler(to: SendTo, message: str = "") -> None:
command="gpt35-continue",
keys=["gpt-continue", "gpt继续"],
desc="继续GPT3.5对话。",
value=33,
)
def gpt35_continue_command_handler(to: SendTo, message: str = "") -> None:
_gptx_continue("gpt-3.5-turbo", to, message)


@command(command="gpt4", keys=["gpt4"], desc="使用GPT4进行对话。", value=40)
@command(
command="gpt4",
keys=["gpt4"],
desc="使用GPT4进行对话。",
)
def gpt4_command_handler(to: SendTo, message: str = "") -> None:
_gptx("gpt-4", to, message)

Expand All @@ -61,7 +66,6 @@ def gpt4_command_handler(to: SendTo, message: str = "") -> None:
command="gpt4-chats",
keys=["gpt4-chats", "gpt4对话记录"],
desc="列出GPT4对话记录。",
value=41,
)
def gpt4_chats_command_handler(to: SendTo, message: str = "") -> None:
_gptx_chats("gpt-4", to, message)
Expand All @@ -71,7 +75,6 @@ def gpt4_chats_command_handler(to: SendTo, message: str = "") -> None:
command="gpt4-record",
keys=["gpt4-record", "gpt4记录"],
desc="获取GPT4对话记录。",
value=42,
)
def gpt4_record_command_handler(to: SendTo, message: str = "") -> None:
_gptx_record("gpt-4", to, message)
Expand All @@ -81,7 +84,6 @@ def gpt4_record_command_handler(to: SendTo, message: str = "") -> None:
command="gpt4-continue",
keys=["gpt4-continue", "gpt4继续"],
desc="继续GPT4对话。",
value=43,
)
def gpt4_continue_command_handler(to: SendTo, message: str = "") -> None:
_gptx_continue("gpt-4", to, message)
Expand Down
5 changes: 3 additions & 2 deletions wechatter/commands/_commands/douyin_hot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
command="douyin-hot",
keys=["抖音热搜", "douyin-hot"],
desc="获取抖音热搜。",
value=50,
)
def douyin_hot_command_handler(to: SendTo, message: str = "") -> None:
try:
Expand All @@ -24,13 +23,15 @@ def douyin_hot_command_handler(to: SendTo, message: str = "") -> None:
else:
Sender.send_msg(to, SendMessage(SendMessageType.TEXT, result))


def get_douyin_hot_str() -> str:
r_json = get_request_json(
url="https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/word/"
)
hot_list = _extract_douyin_hot_data(r_json)
return _generate_douyin_hot_message(hot_list)


def _extract_douyin_hot_data(r_json: Dict) -> List:
try:
hot_list = r_json["word_list"]
Expand All @@ -48,4 +49,4 @@ def _generate_douyin_hot_message(hot_list: List) -> str:
for i, hot_search in enumerate(hot_list[:20]):
hot_str += f"{i + 1}. {hot_search.get('word')}\n"

return hot_str
return hot_str
5 changes: 2 additions & 3 deletions wechatter/commands/_commands/food_calories.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from typing import List, Dict
from typing import Dict, List
from urllib.parse import quote

import requests
from bs4 import BeautifulSoup

from wechatter.commands.handlers import command
from wechatter.exceptions import Bs4ParsingError
from wechatter.models.message import SendMessage, SendMessageType, SendTo
from wechatter.sender import Sender
from wechatter.utils import get_request
from wechatter.exceptions import Bs4ParsingError


@command(
command="food-calories",
keys=["食物热量", "food-calories", "热量", "calories", "卡路里"],
desc="获取食物热量。",
value=150,
)
def food_calories_command_handler(to: SendTo, message: str = "") -> None:
try:
Expand Down
1 change: 0 additions & 1 deletion wechatter/commands/_commands/github_trending.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
command="github-trending",
keys=["GitHub趋势", "github-trending"],
desc="获取GitHub趋势。",
value=60,
)
def github_trending_command_handler(to: SendTo, message: str = "") -> None:
try:
Expand Down
4 changes: 2 additions & 2 deletions wechatter/commands/_commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from wechatter.utils.text_to_image import text_to_image


@command(command="help", keys=["帮助", "help"], desc="获取帮助信息。", value=10)
@command(command="help", keys=["帮助", "help"], desc="获取帮助信息。")
def help_command_handler(to: SendTo, message: str = "") -> None:
# # 获取帮助信息(文本)
# from command.help import get_help_msg
Expand All @@ -23,7 +23,7 @@ def help_command_handler(to: SendTo, message: str = "") -> None:
def get_help_msg() -> str:
help_msg = "=====帮助信息=====\n"
for value in commands.values():
if value["value"] == 0:
if value == "None":
continue
cmd_msg = ""
for key in value["keys"]:
Expand Down
1 change: 0 additions & 1 deletion wechatter/commands/_commands/pai_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
command="pai-post",
keys=["派早报", "pai-post"],
desc="获取少数派早报。",
value=70,
)
def pai_post_command_handler(to: SendTo, message: str = "") -> None:
try:
Expand Down
2 changes: 0 additions & 2 deletions wechatter/commands/_commands/people_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
command="people",
keys=["人民日报", "people", "people-daily"],
desc="获取人民日报。",
value=80,
)
def people_daily_command_handler(to: SendTo, message: str = "") -> None:
"""发送人民日报url"""
Expand Down Expand Up @@ -39,7 +38,6 @@ def people_daily_command_handler(to: SendTo, message: str = "") -> None:
command="people-url",
keys=["人民日报链接", "people-url", "people-daily-url"],
desc="获取人民日报url。",
value=81,
)
def people_daily_url_command_handler(to: SendTo, message: str = "") -> None:
"""发送人民日报url"""
Expand Down
1 change: 0 additions & 1 deletion wechatter/commands/_commands/qrcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
command="qrcode",
keys=["二维码", "qrcode"],
desc="将文本或链接转换为二维码。",
value=90,
)
def qrcode_command_handler(to: SendTo, message: str = "") -> None:
# 获取二维码
Expand Down
1 change: 0 additions & 1 deletion wechatter/commands/_commands/today_in_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
command="today-in-history",
keys=["历史上的今天", "today-in-history", "t-i-h"],
desc="获取历史上的今天。",
value=100,
)
def today_in_history_command_handler(to: SendTo, message: str = "") -> None:
# 获取历史上的今天
Expand Down
2 changes: 0 additions & 2 deletions wechatter/commands/_commands/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
command="todo",
keys=["待办事项", "待办", "todo"],
desc="获取待办事项。",
value=110,
)
def todo_command_handler(to: SendTo, message: str = "") -> None:
# 判断是查询还是添加
Expand All @@ -40,7 +39,6 @@ def todo_command_handler(to: SendTo, message: str = "") -> None:
command="remove-todo",
keys=["删除待办事项", "remove-todo", "rmtd"],
desc="删除待办事项。",
value=111,
)
def remove_todo_command_handler(to: SendTo, message: str = "") -> None:
indices = [
Expand Down
13 changes: 8 additions & 5 deletions wechatter/commands/_commands/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
command="word",
keys=["word", "单词"],
desc="翻译单词或短语。",
value=120,
)
def word_command_handler(to: SendTo, message: str = "") -> None:
from_lang = _detect_lang(message)
Expand Down Expand Up @@ -79,6 +78,7 @@ def word_command_handler(to: SendTo, message: str = "") -> None:

# fmt: on


# 获取翻译字符串
def get_reverso_context_tran_str(content: str, from_lang: str, to_lang: str) -> str:
if not _check_lang_support(from_lang, to_lang):
Expand Down Expand Up @@ -110,7 +110,9 @@ def get_reverso_context_tran_str(content: str, from_lang: str, to_lang: str) ->
else:
logger.info(f"不支持的语言音译:{from_lang}")

message = _generate_translate_message(content, from_lang, to_lang, word_list, transliteration)
message = _generate_translate_message(
content, from_lang, to_lang, word_list, transliteration
)
return message


Expand All @@ -123,10 +125,11 @@ def _extract_transliteration_data(r_json: Dict) -> str:
return transliteration


def _generate_translate_message(content: str, from_lang: str, to_lang: str, word_list: List,
transliteration: str) -> str:
def _generate_translate_message(
content: str, from_lang: str, to_lang: str, word_list: List, transliteration: str
) -> str:
tran_direction_msg = (
LANG_EMOJI_DICT.get(from_lang, "") + "->" + LANG_EMOJI_DICT.get(to_lang, "")
LANG_EMOJI_DICT.get(from_lang, "") + "->" + LANG_EMOJI_DICT.get(to_lang, "")
)
msg = f'({tran_direction_msg}) "{content}" 翻译:\n'
if transliteration != "":
Expand Down
1 change: 0 additions & 1 deletion wechatter/commands/_commands/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
command="weather",
keys=["weather", "天气", "天气预报", "几度"],
desc="获取天气预报",
value=160,
)
def weather_command_handler(to: SendTo, message: str = "") -> None:
try:
Expand Down
4 changes: 3 additions & 1 deletion wechatter/commands/_commands/weibo_hot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from wechatter.sender import Sender
from wechatter.utils import get_request_json


@command(
command="weibo-hot",
keys=["微博热搜", "weibo-hot"],
desc="获取微博热搜。",
value=130,
)
def weibo_hot_command_handler(to: SendTo, message: str = "") -> None:
try:
Expand All @@ -23,13 +23,15 @@ def weibo_hot_command_handler(to: SendTo, message: str = "") -> None:
else:
Sender.send_msg(to, SendMessage(SendMessageType.TEXT, result))


def get_weibo_hot_str() -> str:
r_json = get_request_json(
url="https://m.weibo.cn/api/container/getIndex?containerid=106003%26filter_type%3Drealtimehot"
)
hot_list = _extract_weibo_hot_data(r_json)
return _generate_weibo_hot_message(hot_list)


def _extract_weibo_hot_data(r_json: Dict) -> List:
try:
hot_list = r_json["data"]["cards"][0]["card_group"][:20]
Expand Down
2 changes: 1 addition & 1 deletion wechatter/commands/_commands/zhihu_hot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict, List

from loguru import logger

from wechatter.commands.handlers import command
Expand All @@ -11,7 +12,6 @@
command="zhihu-hot",
keys=["知乎热搜", "zhihu-hot"],
desc="获取知乎热搜。",
value=140,
)
def zhihu_hot_command_handler(to: SendTo, message: str = "") -> None:
try:
Expand Down
4 changes: 1 addition & 3 deletions wechatter/commands/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
"""


def command(command: str, keys: List[str], desc: str, value: int):
def command(command: str, keys: List[str], desc: str):
"""
注册命令
:param command: 命令
:param keys: 命令关键词列表
:param desc: 命令描述
:param value: 命令值
:return: 装饰器
"""

def decorator(func):
commands[command] = {}
commands[command]["keys"] = keys
commands[command]["desc"] = desc
commands[command]["value"] = value
commands[command]["handler"] = func

return func
Expand Down
4 changes: 3 additions & 1 deletion wechatter/init_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from wechatter.utils.path_manager import get_abs_path

# 使用环境变量中的 LOG_LEVEL
LOG_LEVEL = logging.getLevelName(os.environ.get("LOG_LEVEL", "INFO"))
LOG_LEVEL_NAME = os.environ.get("LOG_LEVEL", "INFO")
LOG_LEVEL = logging.getLevelName(LOG_LEVEL_NAME)

LOGURU_FORMAT = (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
Expand Down Expand Up @@ -54,6 +55,7 @@ def init_logger():
{
"sink": sys.stdout,
"format": LOGURU_FORMAT,
"level": LOG_LEVEL_NAME,
}
],
)
Expand Down
Loading

0 comments on commit 763918b

Please sign in to comment.