chatgpt-tegram-bot

A simple bot for Telegram that allows interaction with ChatGPT (with voice notes and support for generating images).

AGPL-3.0 License

Stars
14
Committers
2

chatgpt-telegram-bot

馃寪 English instructions below

Este es un script de Python que permite a un usuario enviar mensajes a un bot de Telegram y recibir respuestas generadas utilizando la API de ChatGPT. Tambi茅n permite a los usuarios generar im谩genes utilizando la API de stability.ia y obtener una transcripci贸n de la respuesta de ChatGPT en formato de nota de voz generada por Amazon Polly.

Requisitos

Para utilizar este script, necesitar谩s:

Todos estos servicios dan cr茅ditos gratuitos de inicio, a Marzo de 2023, estos son los costes de sus APIs:

  • OpenIA API: 0,002$/1K tokens
  • Stability IA: 0,002$/petici贸n (con 30 steps)
  • Amazon Polly: Aprox. 0,0125$/minuto de audio

Instalaci贸n

Para instalar las dependencias necesarias para ejecutar este script en Python, se puede utilizar el administrador de paquetes pip.

Primero, aseg煤rate de tener pip instalado en tu sistema. Puedes verificar si ya lo tienes ejecutando el siguiente comando en una terminal:

pip --version

Si no tienes pip instalado, puedes instalarlo siguiendo las instrucciones en la p谩gina oficial de pip

Para instalar las dependencias, ejecuta el siguiente comando en una terminal en el mismo directorio que el archivo requirements.txt:

pip install -r requirements.txt

Esto instalar谩 todos los paquetes necesarios en tu sistema. Una vez que se hayan instalado, podr谩s ejecutar el script sin problemas.

Configuraci贸n

Antes de ejecutar el script, necesitar谩s configurar las credenciales de acceso para OpenAI, AWS, Telegram y stability.ai. Para hacerlo, renombra el archivo llamado config.py.sample en el mismo directorio que el script Python a config.py y rellena las variables con tus credenciales de acceso.

Importante: EL 12/12/22 el modo web dej贸 de funcionar (detalles t茅cnicos)

Se puede conectar mediante el api de tu cuenta de OpenIA, se controla por la siguiente configuraci贸n:

openai_mode = "api"

Ejecuci贸n

Para ejecutar el script, aseg煤rate de estar en el mismo directorio que el archivo chatgpt-telegram.py y ejecuta el siguiente comando en una terminal:

python chatgpt-telegram.py

Esto iniciar谩 el bot de Telegram. Una vez que est茅 en ejecuci贸n, puedes enviarle mensajes utilizando la aplicaci贸n de Telegram.

El bot acepta los siguientes comandos:

  • /chatgpt: Enviar un mensaje al bot para que genere una respuesta utilizando la API de ChatGPT
  • /draw: Dibujar una imagen utilizando el API de stability.ia
  • /voice: Devuelve la respuesta de ChatGPT, pero adjunta una transcripci贸n como nota de voz generada por Amazon Polly
  • /resetchat: En el modo openai_mode='web' permite reiniciar la conversaci贸n para que no recuerde nada de lo anterior

El bot puede ser tambi茅n incluido en grupos, pero solo responder谩 a los usuarios autorizados que hayas configurado. Ten cuidado dando acceso a otras personas ya que el bot estar谩 usando tus APIs de los servicios y estas suelen tener un coste.

Puedes parar el script en cualquier momento pulsando Ctrl+D

Configurar el bot como un servicio en tu servidor Linux

Para configurar el script chatgpt-telegram.py como un servicio en un servidor Linux, puedes seguir los siguientes pasos:

Nota: Las siguientes instrucciones est谩n optimizadas para un servidor Ubuntu Linux, pueden ser diferentes para otras distribuciones.

  1. Crea un archivo de configuraci贸n para el servicio en el directorio /etc/systemd/system. Puedes hacerlo con el comando sudo nano /etc/systemd/system/chatgpt-telegram.service.
  2. Agrega el siguiente contenido al archivo, reemplazando <RUTA_DEL_SCRIPT> con la ruta completa del script chatgpt-telegram.py en tu servidor:
[Unit]
Description=ChatGPT Telegram Bot Service

[Service]
Type=simple
WorkingDirectory=<RUTA_DEL_SCRIPT>
ExecStart=<RUTA_DEL_SCRIPT>/chatgpt-telegram.py
Restart=on-failure

[Install]
WantedBy=multi-user.target
  1. Guarda y cierra el archivo.
  2. Haz que el sistema operativo cargue la configuraci贸n del nuevo servicio con el comando sudo systemctl daemon-reload.
  3. Inicia el servicio con el comando sudo systemctl start chatgpt-telegram.
  4. Opcionalmente, puedes habilitar el servicio para que se inicie autom谩ticamente cada vez que se inicie el servidor con el comando sudo systemctl enable chatgpt-telegram.

Una vez que hayas seguido estos pasos, el script chatgpt-telegram.py se ejecutar谩 como un servicio en tu servidor. Puedes verificar el estado del servicio con el comando sudo systemctl status chatgpt-telegram.

English

This is a Python script that allows a user to send messages to a Telegram bot and receive responses generated using the ChatGPT API. It also allows users to generate images using the stability.ia API and get a transcript of the ChatGPT response in Amazon Polly generated voice memo format.

Requirements.

To use this script, you will need:

All these services give free startup credits, as of March 2023, these are the costs of their APIs:

  • OpenIA: $0.002/1K tokens.
  • Stability IA: 0,002$/request (with 30 steps)
  • Amazon Polly: Approx. $0.0125/audio minute

Installation

To install the dependencies needed to run this Python script, you can use the pip package manager.

First, make sure you have pip installed on your system. You can check if you already have it by running the following command in a terminal:

pip --version

If you don't have pip installed, you can install it by following the instructions on the official pip homepage

To install the dependencies, run the following command in a terminal in the same directory as the requirements.txt file:

pip install -r requirements.txt

This will install all the necessary packages on your system. Once they are installed, you will be able to run the script without any problems.

Configuration

Before running the script, you will need to configure the login credentials for OpenAI, AWS, Telegram and stability.ai. To do this, rename the file named config.py.sample in the same directory as the Python script to config.py and fill in the variables with your login credentials.

Important: On 12/12/22 web mode stopped working (technical details)

It can be connected via OpenIA api, it is controlled by the following configuration:

openai_mode = "api"

Execution

To run the script, make sure you are in the same directory as the chatgpt-telegram.py file and run the following command in a terminal:

python chatgpt-telegram.py

This will start the Telegram bot. Once it is running, you can send messages to it using the Telegram app.

The bot accepts the following commands:

  • /chatgpt: Send a message to the bot to generate a response using the ChatGPT API.
  • /draw: Draw an image using the stability.ia API
  • /voice: Return the ChatGPT response, but attach a transcript as a voice memo generated by Amazon Polly.
  • /resetchat: In openai_mode='web' mode allows you to restart the conversation so that it does not remember anything of the previous one.

The bot can also be included in groups, but will only respond to authorized users that you have configured. Be careful giving access to other people as the bot will be using your services APIs and these usually come at a cost.

You can stop the script at any time by pressing Ctrl+D

Configuring the bot as a service on your Linux server

To configure the chatgpt-telegram.py script as a service on a Linux server, you can follow the steps below:

Note: The following instructions are optimized for an Ubuntu Linux server, they may be different for other distributions.

Create a configuration file for the service in the /etc/systemd/system directory. You can do this with the command sudo nano /etc/systemd/system/chatgpt-telegram.service. 2. Add the following content to the file, replacing <SCRIPT_PATH> with the full path to the chatgpt-telegram.py script on your server:

[Unit]
Description=ChatGPT Telegram Bot Service

[Service]
Type=simple
WorkingDirectory=<RUTA_DEL_SCRIPT>
ExecStart=<RUTA_DEL_SCRIPT>/chatgpt-telegram.py
Restart=on-failure

[Install]
WantedBy=multi-user.target
  1. Save and close the file.
  2. Have the operating system load the new service configuration with the command sudo systemctl daemon-reload.
  3. Start the service with the command sudo systemctl start chatgpt-telegram.
  4. Optionally, you can enable the service to start automatically every time the server starts with the command sudo systemctl enable chatgpt-telegram.

Once you have followed these steps, the chatgpt-telegram.py script will run as a service on your server. You can check the status of the service with the command sudo systemctl status chatgpt-telegram.

Related Projects