Skip to content

Commit

Permalink
feat: telegraph
Browse files Browse the repository at this point in the history
  • Loading branch information
xingty committed Jun 30, 2024
1 parent 9be49e5 commit 61f4382
Show file tree
Hide file tree
Showing 21 changed files with 598 additions and 42 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,12 @@ ENV/
.pdm-python
.pdm-build/

data.db
data*.db
config.json
endpoints.json
sessions/
logs/*
test/*
file::*
file::*
telegraph.json
conversation_preview.json
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ CatGPT is a Telegram bot that integrates with OpenAI's api for people who like t

![new topic](assets/new.png)

* **/topic** `[share | download | title]`
* **/topic** `[share | download | iv | inner | title]`

There are three optional operations that you can use to perform some task quickly.

Expand All @@ -86,9 +86,19 @@ CatGPT is a Telegram bot that integrates with OpenAI's api for people who like t

Use `/topic download` to download the topic's content directly without a confirmation operation. The content will be encoded as Markdown file

* **other characters**
* `iv`

Any other characters will be treated as a title, and the topic's title will be updated."
Display the chat history of this conversation in the form of Instant View even if the text length does not overflow.

![instant view](assets/iv.png)
* `inner`

Display the chat history of this conversation in the form of Telegram Message.
* **other characters**

Any other characters will be treated as a title, and the topic's title will be updated."


![operations](assets/dl_share.png)
Expand Down Expand Up @@ -176,6 +186,8 @@ options:

* `respond_group_message`: If true, the bot will respond to group messages even if it is not mentioned. default: `false`, can be changed at runtime using the command `/respond` in groups.

* `topic_preview_type`: **TELEGRAPH** or **INTERNAL**

* `endpoinds`: your endpoints

endpint:
Expand All @@ -202,6 +214,7 @@ options:
"access_key": "Specify Access Key to use this bot",
"proxy": "http://proxy:port",
"respond_group_message": false,
"topic_preview_type": "INTERNAL or TELEGRAPH",
"share": [
{
"name": "notes",
Expand Down
Binary file added assets/iv.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
"access_key": "Specify Access Key to use this bot",
"proxy": "http://proxy:port",
"respond_group_message": false,
"topic_preview_type": "INTERNAL | TELEGRAPH",
"share": [
{
"name": "share provider name",
"type": "github",
"repo": "github repo",
"owner": "your github username",
"token": "your github personal access token"
},
{
"name": "telegraph",
"type": "telegraph",
"author": "meiqiu(optional)",
"short_name": "meiqiu(optional)",
"token": "your telegraph token, it will create a new account if you don't provide one"
}
],
"endpoints": [
Expand Down
81 changes: 80 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ dependencies = [
"openai==1.6.0",
"aiohttp==3.9.1",
"telegramify-markdown==0.1.4",
"google-generativeai==0.7.0"
"google-generativeai==0.7.0",
"markdown",
"bs4"
]
requires-python = ">=3.9"
readme = "README.md"
Expand Down
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ annotated-types==0.7.0
anyio==4.4.0
async-timeout==4.0.3; python_version < "3.11"
attrs==23.2.0
beautifulsoup4==4.12.3
bs4==0.0.2
cachetools==5.3.3
certifi==2024.6.2
charset-normalizer==3.3.2
Expand All @@ -29,6 +31,8 @@ httpcore==1.0.5
httplib2==0.22.0
httpx==0.27.0
idna==3.7
importlib-metadata==8.0.0; python_version < "3.10"
markdown==3.6
markdown-it-py==3.0.0
mdurl==0.1.2
mistletoe==1.3.0
Expand All @@ -45,9 +49,11 @@ pytelegrambotapi==4.18.1
requests==2.32.3
rsa==4.9
sniffio==1.3.1
soupsieve==2.5
telegramify-markdown==0.1.4
tqdm==4.66.4
typing-extensions==4.12.2
uritemplate==4.1.1
urllib3==2.2.2
yarl==1.9.4
zipp==3.19.2; python_version < "3.10"
25 changes: 18 additions & 7 deletions src/catgpt/commands/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from telebot.asyncio_helper import ApiTelegramException
from telebot.types import Message

from ..context import profiles, config, get_bot_name, topic, group_config
from ..types import Endpoint, MessageType
from ..context import profiles, config, get_bot_name, topic, group_config, page_preview
from ..types import Endpoint, MessageType, Preview
from ..utils.text import get_timeout_from_text, MAX_TEXT_LENGTH
from . import create_convo_and_update_profile
from ..provider import ask, ask_stream
Expand Down Expand Up @@ -107,7 +107,7 @@ async def handle_message(message: Message, bot: AsyncTeleBot) -> None:

text = ""
try:
text = await do_reply(endpoint, model, messages, reply_msg, bot)
text = await do_reply(endpoint, model, messages, reply_msg, bot, convo)
reply_msg.text = text
await topic.append_messages(convo_id, message, reply_msg)

Expand All @@ -131,6 +131,7 @@ async def do_reply(
messages: list,
reply_msg: Message,
bot: AsyncTeleBot,
convo: types.Topic,
):
text = ""
buffered = ""
Expand Down Expand Up @@ -185,14 +186,24 @@ async def do_reply(
else:
raise ae

# if len(buffered) > 0: for removing the endpoint info from the message
delta = timeout - (time.time() - start)
if delta > 0:
await asyncio.sleep(int(delta) + 1)

text += buffered
msg_text = escape(text)
msg_text = escape(text + buffered)
if text_overflow or len(msg_text) > MAX_TEXT_LENGTH:
if config.topic_preview == Preview.TELEGRAPH:
msg_text = text + buffered
title = f"{convo.title}_{reply_msg.message_id}"
url = await page_preview.preview_chat(convo.label, title, msg_text)
await bot.edit_message_text(
chat_id=reply_msg.chat.id,
message_id=reply_msg.message_id,
text=url,
disable_web_page_preview=False,
)
return msg_text

text_overflow = True
msg_text = escape(text)

Expand All @@ -211,7 +222,7 @@ async def do_reply(
reply_to_message_id=msg.message_id,
)

return text
return text + buffered


async def do_generate_title(convo: types.Topic, messages: list, uid: int, text: str):
Expand Down
Loading

0 comments on commit 61f4382

Please sign in to comment.