Skip to content

Commit

Permalink
Update Discord and Telegram clients with proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
Feiyuyu0503 committed Feb 2, 2024
1 parent 1ff3992 commit a9e7d45
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Bots/discordClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
import asyncio
from config import config
from loguru import logger
from aiohttp import BasicAuth

class DiscordBotClient(BotClient):
def __init__(self, token: str, channel_id: int,dalle_bot_id: int):
intents = discord.Intents.default()
intents.message_content = True
self.client = discord.Client(intents=intents)
self.client = discord.Client(intents=intents,proxy=config.Proxy,proxy_auth=BasicAuth(*config.Proxy_Auth.split(':')) if config.Proxy_Auth else None)
self.token = token
self.channel_id = channel_id
self.pending_responses = {} # 用于存储消息 ID 和(事件,回复消息)
self.dalle_bot = dalle_bot_id

@self.client.event
async def on_ready():
logger.info("Discord client started! We have logged in as {self.client.user}!")
logger.info(f"Discord client started! We have logged in as {self.client.user}!")

async def start(self):
self.client.event(self.on_message)
Expand Down
7 changes: 6 additions & 1 deletion Bots/teleClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
from loguru import logger
from collections import deque
import base64,uuid
import python_socks

class TelegramBotClient(BotClient):
def __init__(self, api_id: int, api_hash: str, bot_username: str):
self.client = TelegramClient(StringSession(config.SESSION_STRING), api_id, api_hash)
client_proxy_addr,client_proxy_port = config.Proxy.split(':')[1:] if config.Proxy else (None,None)
client_proxy_auth_user,client_proxy_auth_password = config.Proxy_Auth.split(':') if config.Proxy_Auth else (None,None)
# https://stackoverflow.com/a/71668186
client_proxy = (python_socks.ProxyType.HTTP,client_proxy_addr[2:],int(client_proxy_port),True,client_proxy_auth_user,client_proxy_auth_password) if config.Proxy else None
self.client = TelegramClient(StringSession(config.SESSION_STRING), api_id, api_hash,proxy=client_proxy)
self.bot_username = bot_username
self.pending_responses = {} # 用于存储消息 ID 和(事件,回复消息)
self.pending_responses_queue = deque()
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM python:3.9-slim
LABEL author="feiyuyu"
LABEL email="[email protected]"
LABEL version="0.3.5"
LABEL version="0.3.8"

# 设置工作目录为/app
WORKDIR /app
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Free DALL·E Proxy
<img src=".github\images\demo.webp" width="50%" height="50%"><img src=".github\images\demo2.webp" width="50%" height="50%">

### English · [简体中文](./README.zh-CN.md)
### English · [简体中文](./README.zh-CN.md) · [Changelog](./docs/CHANGELOG.md)
## Introduction
`free-dall-e-proxy` is a project that leverages the power of bots supported by the [coze](https://www.coze.com) (currently Telegram and Discord) to provide free access to OpenAI's DALL·E 3 image generation capabilities. This proxy service offers an OpenAI-standard API endpoint, allowing developers to easily integrate DALL·E 3 into their applications.

Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Free DALL·E Proxy
<img src=".github\images\demo.webp" width="50%" height="50%"><img src=".github\images\demo2.webp" width="50%" height="50%">

### [English](./README.md) · 简体中文
### [English](./README.md) · 简体中文 · [更新记录](./docs/CHANGELOG.md)
## 介绍
`free-dall-e-proxy`是利用由[coze](https://www.coze.com)支持的机器人(目前是Telegram和Discord),提供免费访问OpenAI的DALL·E 3图像生成的代理服务。项目提供了一个符合OpenAI标准的API端点,允许开发者轻松地将此DALL·E 3代理服务集成到他们的应用程序中。

Expand Down
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dotenv import load_dotenv
import os
from loguru import logger
from aiohttp import BasicAuth

class Config:
# Load .env file
Expand All @@ -11,6 +12,8 @@ class Config:
logger.warning(f"the env 'FASTAPI_SERVER_URL' is deprecated, please remove it and set 'PORT' in your data/.env instead.(Default: PORT=8000)")
PORT = int(os.getenv('PORT') or 8000)
Timeout = float((os.getenv('TIMEOUT')) or 60)
Proxy = os.getenv('PROXY')
Proxy_Auth = os.getenv('PROXY_AUTH')
# telegram
TELEGRAM = os.getenv('TELEGRAM')
API_ID = int(os.getenv('API_ID') or -1)
Expand Down
6 changes: 4 additions & 2 deletions data/.env.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# If you want to custom the 'PORT' value when using docker, you should pass '-e PORT=your_port_num' to docker run command.
PORT=8000 # replace with your fastapi server port, default is 8000.
TIMEOUT=60 # if the response time exceeds this value, the request will be aborted
#PROXY=http://127.0.0.1:7890 # you should set the proxy if you cannot connect to discord or telegram
#PROXY_AUTH=username:password # set the proxy auth if your proxy requires auth

#telegram
# telegram
TELEGRAM=True # set to True if you want to use coze-telegram bot
API_ID=23333333 # replace with your api id
API_HASH=96z7a292723306fdgb2338d2338cr6d6 # replace with your api hash
BOT_USERNAME=@dalle_bot # replace with your bot username
SESSION_NAME=whatever # replace with your session name
#SESSION_STRING=2BctsOKw8u7koN...NqHR5ehWjJ8N60z... # you can set this env if you have already got a telegram string session

#discord
# discord
DISCORD=False # set to True if you want to use coze-discord bot
DISCORD_ClIENT_BOT_TOKEN=MTE52330uoY0ZAQ3NjE1NztxOQ.2334P1.D8z6FuppNs23380xbs2ak_ih2GFNPt_yt1QgL6 # replace with your discord bot token
DISCORD_CHANNEL_ID=1206832685596528246 # replace with your discord channel id
Expand Down
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
### 2024-2-2 version="0.3.8"
1. Discord、Telegram支持设置代理登录 https://stackoverflow.com/a/71668186

### 2024-2-1 version="0.3.5"
1. 适配Telegram对发送图片行为的改动
2. 通过Telgram平台生成的图片会保存在data/images
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Telethon==1.33.1
uvicorn==0.26.0
aiofiles==23.2.1
loguru==0.7.2
cryptg==0.4.0
cryptg==0.4.0
python-socks[asyncio]==2.4.4

0 comments on commit a9e7d45

Please sign in to comment.