This commit is contained in:
Anton Palgunov 2023-03-31 21:00:00 +01:00
commit 693d484872
4 changed files with 280 additions and 0 deletions

174
.gitignore vendored Normal file
View File

@ -0,0 +1,174 @@
# standard gitignore file for python projects
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# dotenv
.env
# virtualenv
.venv
venv/
ENV/
# Spyder project settings
.spyderproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# profiling data
.profraw
.profdata
# pycharm
.idea
# vscode
.vscode
#vscode
.vscode
# build
build
# docs
docs
# jupyter
.ipynb_checkpoints
# pycache
__pycache__
# pyproject
pyproject.toml
# pytest
pytest_cache
# tox
tox
# mypy
mypy_cache
# pyre
.pyre
# pytype
.pytype
# profiling
.profraw
.profdata
# pycharm
.idea
# vscode
.vscode
# build
build
# docs
docs
# jupyter
.ipyn
.ipynb

60
main.py Normal file
View File

@ -0,0 +1,60 @@
import os
import telebot
import openai
from dotenv import load_dotenv
# Load the environment variables from the .env file
load_dotenv()
# Load the OpenAI API key from the environment variable
openai.api_key = os.environ.get("OPENAI_API_KEY")
# Load the Telegram bot token from the environment variable
bot = telebot.TeleBot(os.environ.get("TELEGRAM_BOT_TOKEN"))
# Define a function to generate a response using the ChatGPT model
def generate_response(message_text):
# Call the OpenAI API to generate a response
response = openai.Completion.create(
engine="text-davinci-003",
prompt=message_text,
max_tokens=1024,
n=1,
stop=None,
temperature=0.7,
)
# print all the response
# print(response)
# Extract the generated text from the response and return it
return response.choices[0].text.strip()
# Define a handler function for incoming messages
@bot.message_handler(func=lambda message: True)
def handle_message(message):
print(message.from_user.id, message.from_user.first_name, message.from_user.last_name, message.from_user.username, message.text)
# check if the message comming from user id 210098655 Коля or 40196122 Я
if message.from_user.id != 210098655 and message.from_user.id != 40196122:
# Send a message to the user
bot.reply_to(message, "Я не узнаю тебя, ты кто?")
return
# send confirm message to user
# bot.reply_to(message, "Опять работать...")
# send typing message to user
bot.send_chat_action(message.chat.id, 'typing')
# Generate a response using the ChatGPT model
response_text = generate_response(message.text)
print(response_text)
# Send the response back to the user
bot.reply_to(message, response_text)
# Start the bot and listen for incoming messages
bot.polling()

44
readme.md Normal file
View File

@ -0,0 +1,44 @@
# Telegram Bot with ChatGPT
This is a simple Telegram bot that uses the ChatGPT model to generate responses to user messages. The bot is built using Python and the pyTelegramBotAPI package for interacting with the Telegram Bot API, and the OpenAI API for generating responses using the ChatGPT model.
## Prerequisites
To run this bot, you will need:
- Python 3.6 or higher
- A Telegram bot token (you can obtain this by following the instructions [here](https://core.telegram.org/bots#creating-a-new-bot))
- An OpenAI API key (you can obtain this by signing up for an account [here](https://beta.openai.com/signup/))
## Installation
To install the required Python packages, run the following command:
`pip install -r requirements.txt`
This will install the `pyTelegramBotAPI` and `openai` packages, which are needed to run the bot.
## Usage
To start the bot, run the following command:
`python main.py`
This will start the bot and it will begin listening for incoming messages from users. Whenever a user sends a message, the bot will use the ChatGPT model to generate a response and send it back to the user.
## Configuration
To configure the bot, you will need to edit the `bot.py` file and replace the following placeholders with your own values:
- `YOUR_TELEGRAM_BOT_TOKEN_HERE`: replace this with your Telegram bot token
- `YOUR_OPENAI_API_KEY_HERE`: replace this with your OpenAI API key
You can also modify the `generate_response` function in the `bot.py` file to customize how the ChatGPT model generates responses.
## Contributing
If you find any issues or have any suggestions for improving the bot, please feel free to open an issue or submit a pull request.
## License
This project is licensed under the MIT License - see the `LICENSE` file for details.

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
pyTelegramBotAPI
openai