• Stars
    star
    313
  • Rank 133,714 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 2 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Humble GPT Telegram Bot

ChatGPT Telegram Bot

This is a Telegram chat bot built using the ChatGPT (GPT-3.5 or GPT-4) language model from OpenAI.

Notable features:

  • Both one-on-one and group chats.
  • Direct questions, mentions, follow-ups.
  • Access external links (articles, code, data).
  • Shortcuts (custom AI commands).
  • Image generation.
  • On-the-fly configuration.

Read on for a detailed feature description or jump to setup.

Personal chats

The bot acts as your personal assistant:

Sample chat

To allow other users to use the bot, list them in the telegram.usernames config property.

The bot has a terrible memory, so don't expect it to remember any chat context by default. You can, however, reply with a follow-up question (Ctrl/Cmd + ↑). Alternatively, use a plus sign to follow up:

Follow-up by reply Follow-up by plus sign

Available commands:

  • /retry - retry answering the last question
  • /help - show help
  • /version - show bot info

To rephrase or add to the last question, simply edit it. The bot will then answer the updated question.

Groups

To get an answer from the bot in a group, mention it in a reply to a question, or ask a question directly:

Reply with mention Direct question

To make the bot reply to group members, list the group id in the telegram.chat_ids config property. Otherwise, the bot will ignore questions from group members unless they are listed in the telegram.usernames config property.

If you don't know the group id, run the /version bot command in a group to find it:

Chat information:
- id: -1001405001234
- title: My Favorite Group
- type: supergroup
...

External links

If you ask "vanilla" ChatGPT about external resources, it will either hallucinate or admit that it doesn't have access to remote content:

Q: What is the content of https://sqlime.org/employees.sql? Make no assumptions.

A: As an AI language model, I cannot access external URLs on the internet.

The bot solves the problem by fetching the remote content and feeding it to the model:

External links

Currently only supports text content (articles, code, data), not PDFs, images or audio.

Shortcuts

Use short commands to save time and ask the bot to do something specific with your questions. For example, ask it to proofread your writing with a !proofread command:

Shortcuts

There are several built-in shortcuts:

  • bugfix fixes your code.
  • proofread fixes your writing.
  • translate translates your text into English.
  • summarize gives a two paragraph summary of a text.

You can add your own shortcuts. See config.example.yml for details.

Image generation

Use the /imagine command to generate an image using the DALL-E 2 model from OpenAI:

Generated image

The default image size is 512×512 px. Other supported sizes are 256×256 and 1024×1024:

/imagine a lazy cat on a sunny day 256x256

/imagine a lazy cat on a sunny day 1024px

Image generation is quite pricey ($0.016-$0.020 per image). By default it's only enabled for users listed in telegram.usernames, not for group members. You can change this with the imagine.enabled config property.

Other useful features

The convenience of working with a bot is made up of small details. Here are some situations where it can save you time and effort.

Forwarding

Say you received a message from a colleague or read a post on a channel and want to ask a question. Simply forward the message to the bot and answer the clarifying question it asks:

Forwarding 1 Forwarding 2

Reply with attachment

Sometimes the AI's reply exceeds the maximum message length set by Telegram. In this case, the bot will not fail or spam you with messages. Instead, it will send the answer as an attached markdown file:

Reply with attachment

Edited question

To rephrase or add to the last question, edit it ( shortcut). The bot will notice this and respond to the clarified question.

Bot information

Use the /version command to print detailed information about the current chat, bot, and AI model:

Chat information:
- id: -1001405001234
- title: My Favorite Group
- type: supergroup

Bot information:
- id: 5930739038
- name: @pokitokibot
- version: 129
- usernames: 6 users
- admins: 1 users
- chat IDs: []
- access to messages: True

AI information:
- model: gpt-3.5-turbo
- history depth: 3
- imagine: True
- shortcuts: bugfix, proofread, summarize, translate

Configuration

Use the /config command to change almost any setting on the fly, without restarting the bot.

  • Add or remove users and chats allowed to interact with the bot (telegram.usernames and telegram.chat_ids).
  • Adjust the AI model (openai.model), prompt (openai.prompt) and params (openai.params).
  • Enable or disable image generation (imagine.enabled).
  • Add or change AI shortcuts (shortcuts).
  • Change any other config property.

To view a specific config property, put its name after /config:

/config telegram.usernames
/config openai.prompt
/config imagine.enabled

To change a specific config property, put its name and value after /config:

/config telegram.usernames ["alice", "bob", "cindy"]
/config openai.prompt "You are an evil AI bot"
/config imagine.enabled none

When working with list properties like telegram.usernames, you can add or remove individual items instead of redefining the whole list:

/config telegram.usernames +cindy
/config telegram.usernames -bob

The /config command is only available to admins - users listed in the telegram.admins property.

Message limits

Heated discussions with the bot in large groups can lead to high usage of the OpenAI API. To avoid spending your entire budget, set message limits for groups with the conversation.message_limit config property.

You can limit the number of messages from a user over a period of time. For example:

  • 10 messages per user per hour:
message_limit:
    count: 10
    period: hour
  • 1 message per user per minute:
message_limit:
    count: 1
    period: minute
  • 30 messages per user per day:
message_limit:
    count: 30
    period: day

Setup

  1. Get your OpenAI API key

  2. Get your Telegram bot token from @BotFather

  3. Copy config.example.yml to config.yml and specify your tokens there.

  4. Start the bot:

docker compose up --build --detach

To stop the bot:

docker compose stop

For older Docker distributions, use docker-compose instead of docker compose.

Development setup

Prepare the environment:

python3 -m venv env
. env/bin/activate
pip install -r requirements.txt

Prepare the config and data directory:

cp config.example.yml config.yml
mkdir ./data

Set tokens and other settings in config.yml.

Run tests:

python -m unittest discover

Start the bot:

python -m bot.bot

Contributing

For new features and improvements, please first open an issue to discuss what you would like to change.

Be sure to add or update tests as appropriate.

Use Black for code formatting and Conventional Commits for commit messages.

Changelog

For feature history, see releases.

Credits

Originally based on chatgpt_telegram_bot, although the projects have diverged quite a bit.

More Repositories

1

sqlean

The ultimate set of SQLite extensions
C
3,638
star
2

redka

Redis re-implemented with SQLite
Go
3,354
star
3

codapi

Interactive code examples for documentation, education and fun
Go
1,436
star
4

sqlime

Online SQLite playground
JavaScript
890
star
5

codapi-js

Interactive code examples for documentation, education and fun.
JavaScript
478
star
6

dataviz

Data visualization guide for presentations, reports, and dashboards
Python
436
star
7

sqlpkg-cli

SQLite package manager
Go
199
star
8

zero

Zero Dependency Club
159
star
9

sqlpkg

SQLite extension registry
132
star
10

iuliia-py

Transliterate Cyrillic → Latin in every possible way
Python
112
star
11

sqlean.py

Python's sqlite3 + extensions
C
105
star
12

sqlean.js

Browser-based SQLite with extensions
Makefile
86
star
13

iuliia-js

Transliterate Cyrillic → Latin in every possible way
TypeScript
82
star
14

uuidv7

UUIDv7 in N languages
Racket
79
star
15

podsearch-py

Let's find some podcasts!
Python
72
star
16

iuliia

Transliterate Cyrillic → Latin in every possible way
69
star
17

sqlite

SQLite / Sqlean shell builds
Makefile
62
star
18

git-by-example

Step-by-step guide to Git operations
47
star
19

whatbrowser

Все, что нужно знать о браузере пользователя
JavaScript
43
star
20

tryxinyminutes

Live code tutorials in your browser
Shell
41
star
21

invoice

Ну очень простое выставление счетов для юрлиц
HTML
39
star
22

words

Word and phrase lists in CSV
Python
30
star
23

grep-by-example

Step-by-step guide to grep operations
28
star
24

metro

Metro lines and stations for cities in Belorussia, Kazakhstan, Russia and Ukraine
14
star
25

runno

Runno WASI runtime.
TypeScript
10
star
26

sqlite-stats

Statistical functions for SQLite
C
9
star
27

sqlite-wasi

SQLite WASI build.
C
9
star
28

metalsmith-typography

A Metalsmith plugin to enhance typography
JavaScript
9
star
29

python-wasi

Python WASI build.
JavaScript
8
star
30

php-wasi

PHP WASI build.
JavaScript
8
star
31

curl-by-example

Step-by-step guide to curl operations
8
star
32

sqliter

SQLite на практике
JavaScript
6
star
33

lua-wasi

Lua WASI build.
C
5
star
34

nalgeon.github.io

Everything about SQLite, Python, open data and awesome software
JavaScript
3
star
35

sqlite-ext

All "miscellaneous" SQLite extensions from sqlite.org
Makefile
3
star
36

asylum

«Психбольница» Купера 20 лет спустя
CSS
2
star
37

nalgeon

2
star
38

sql-window-functions

SQL Window Functions Explained
2
star
39

inline-css

A native CSS framework for rapid UI development.
HTML
1
star
40

wee

Toy WSGI-compatible web framework
Python
1
star
41

rosenc

HTML
1
star
42

ruby-wasi

Ruby WASI build.
JavaScript
1
star
43

actionist

GitHub Actions Playground
C
1
star