Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Telegram bot backed by OpenAI APIs

License

NotificationsYou must be signed in to change notification settings

epieffe/telegram-openai

Repository files navigation

Node.jsAGPLv3

Telegram bot backed by OpenAIChat Completions andImage generations API.

Overview

Private chat commands

  • /chat: start a chat conversation
  • /draw: start a drawing conversation
  • /exit: exit current conversation

Group chat commands

  • /chat <question>: ask a question
  • /draw <subject>: generate an image

Conversations are not available in group chats, so the bot does not remember previously asked questions, while in private/chat conversations it remembers previous messages until you/exit the conversation.

Getting Started

To run the bot you need the following requirements:

Obtain Telegram Bot Token

Contact the@BotFather on Telegram, send the/newbot command and follow the steps.

Obtain OpenAI API Key

Environment Variables

Environment variables can be read from the operating system or from a.env file in the current directory. You can use the.env.example file as a starting point to create your own.env file. Some variables are optional, while others are required for the bot to start properly.

VariableDescriptionRequired
TELEGRAM_TOKENThe Telegram bot tokentrue
OPENAI_API_KEYThe OpenAI API keytrue
CHAT_PROMPT_PATHRelative file path for a custom chat system prompt, defaults toprompt.txtfalse
WHITELISTED_GROUP_IDSComma separated ids of groups whose members are allowed to chat with the botfalse
WHITELISTED_USER_IDSComma separated ids of users allowed to chat with the botfalse

Run the Bot

Before running the bot for the first time you need to install the required dependencies:

npm install

Create a.env file or set the required environment variables in the operating system. If you want to create the.env file you can use the.env.example file as an example. If you want to use OS environment variables you can run the following commands on UNIX systems like Mac or Linux, replacingyour-token andyour-key with actual values:

export TELEGRAM_TOKEN=your-tokenexport OPENAI_API_KEY=your-key

Now you are ready to start the bot:

npm start

Production build

For running in production it is better to use thebuild script to generate a production build

npm run build

Now run the generated build files usingnode:

node build/src/main.js

Run with Docker

You can use theepieffeeth/telegram-openai image on Docker Hub to run this bot using Docker:

docker run epieffeeth/telegram-openai \        -e TELEGRAM_TOKEN=your-token \        -e OPENAI_API_KEY=your-key

Custom Chat System Prompt

You can optionally define a system message to set the behavior of the bot during chats. For example, you can modify the personality of the bot or provide it with specific knowledge about your organization.

By default the bot searches for a system message in theprompt.txt file in the current directory during startup. If not found, no system message is used. To change the file path where to search the system message use theCHAT_PROMPT_PATH environment variable.

If you run the bot with Docker place the prompt file under/app/prompt.txt in the container:

docker run epieffeeth/telegram-openai \        -e TELEGRAM_TOKEN=your-token \        -e OPENAI_API_KEY=your-key \        -v"$PWD/prompt.txt":/app/prompt.txt

Access Restrictions

By default the bot is accessible to anyone. You can restrict the bot access to a limited set of users or to members of specific groups.

The bot is accessible to all the users listed in theWHITELISTED_USER_IDS environment variable AND to members of any of the groups listed in theWHITELISTED_GROUP_IDS environment variable. If none of these variables are set, then the bot is accessible to anyone.

The bot needs to be member of all the groups listed in theWHITELISTED_GROUP_IDS environment variable.

Get your user id

The easiest way to get your user id is to contact the@userinfobot, send a message and it will reply with your info, including the user id. Please note that I am not in any way affiliated with the @userinfobot and I am not responsible for how it works or how it manages data.

Get group id

Please readthis thread on Stack Overflow for usefull tips on how to get a group chat id.

License

This work is licensed under theAGPLv3. VisitWhy the Affero GPL on gnu.org to know why.


[8]ページ先頭

©2009-2025 Movatter.jp