Skip to content

Commit

Permalink
1. Fixed bug: potential timeout issues
Browse files Browse the repository at this point in the history
2. Update README document

3. Add insufficient balance error handling

4. Fixed bug: potential webhook unresponsive issues
  • Loading branch information
yym68686 committed Dec 20, 2023
1 parent 87f3e43 commit d9525b0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ Join the [Telegram Group](https://t.me/+_01cz9tAkUc1YzZl) chat to share your use
| ---------------------- | ------------------------------------------------------------ |
| **BOT_TOKEN (required)** | Telegram bot token. Create a bot on [BotFather](https://t.me/BotFather) to get the BOT_TOKEN. |
| **API (required)** | OpenAI or third-party API key. |
| GPT_ENGINE (optional) | Set the default QA model; the default is:`gpt-4-1106-preview`. This item can be freely switched using the bot's "info" command, and it doesn't need to be set in principle. |
| WEB_HOOK (optional) | Whenever the telegram bot receives a user message, the message will be passed to WEB_HOOK, where the bot will listen to it and process the received messages in a timely manner. |
| API_URL(optional) | If you are using the OpenAI official API, you don't need to set this. If you using a third-party API, you need to fill in the third-party proxy website. The default is: https://api.openai.com/v1/chat/completions |
| GPT_ENGINE (optional) | Set the default QA model; the default is:`gpt-3.5-turbo`. This item can be freely switched using the bot's "info" command, and it doesn't need to be set in principle. |
| NICK (optional) | The default is empty, and NICK is the name of the bot. The bot will only respond when the message starts with NICK that the user inputs, otherwise the bot will respond to any message. Especially in group chats, if there is no NICK, the bot will reply to all messages. |
| PASS_HISTORY (optional) | The default is true. The bot remembers the conversation history and considers the context when replying next time. If set to false, the bot will forget the conversation history and only consider the current conversation. |
| GOOGLE_API_KEY (optional)| If you need to use Google search, you need to set it. If you do not set this environment variable, the bot will default to provide duckduckgo search. Create credentials in Google Cloud's [APIs & Services](https://console.cloud.google.com/apis/api/customsearch.googleapis.com) and the API Key will be GOOGLE_API_KEY on the credentials page. Google search can be queried 100 times a day, which is completely sufficient for light use. When the usage limit has been reached, the bot will automatically turn off Google search. |
Expand Down
4 changes: 2 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,9 @@ async def post_init(application: Application) -> None:

if WEB_HOOK:
print("WEB_HOOK:", WEB_HOOK)
application.run_webhook("127.0.0.1", PORT, webhook_url=WEB_HOOK)
application.run_webhook("0.0.0.0", PORT, webhook_url=WEB_HOOK)
else:
# application.run_polling()
time_out = 600
application.run_polling(read_timeout=time_out, write_timeout=time_out)
application.run_polling(read_timeout=time_out, write_timeout=time_out, pool_timeout=time_out, connect_timeout=time_out)
# application.run_polling(read_timeout=time_out, write_timeout=time_out, pool_timeout=time_out, connect_timeout=time_out, timeout=time_out)
6 changes: 5 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
from utils.chatgpt2api import Chatbot as GPT
from utils.chatgpt2api import Imagebot, claudebot
if API:
ChatGPTbot = GPT(api_key=f"{API}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature)
try:
ChatGPTbot = GPT(api_key=f"{API}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature)
except:
ChatGPTbot = GPT(api_key=f"{API}", engine="gpt-3.5-turbo-1106", system_prompt=systemprompt, temperature=temperature)

try:
GPT4visionbot = GPT(api_key=f"{API}", engine="gpt-4-vision-preview", system_prompt=systemprompt, temperature=temperature)
except:
Expand Down
8 changes: 6 additions & 2 deletions utils/chatgpt2api.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,12 @@ def get_message_token(self, url, json_post):
if response.status_code != 200:
json_response = json.loads(response.text)
string = json_response["error"]["message"]
print(json_response, string)
string = re.findall(r"\((.*?)\)", string)[0]
print(json_response)
try:
string = re.findall(r"\((.*?)\)", string)[0]
except:
if "You exceeded your current quota" in json_response:
raise Exception("当前账号余额不足!")
numbers = re.findall(r"\d+\.?\d*", string)
numbers = [int(i) for i in numbers]
if len(numbers) == 2:
Expand Down

0 comments on commit d9525b0

Please sign in to comment.