Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bot: implement basic features in bot #1

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 4 additions & 129 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,129 +1,4 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.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/
.nox/
.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

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
.token.sh
log
util_test.sh
bot.log
86 changes: 85 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,85 @@
# fosscu-telegram-bot
# FOSSCU-K Telegram Bot

FOSSCU-K Bot is a Telegram bot written in Bash that interacts with GitHub to fetch and display organization members, open issues, and pull requests. Additionally, it provides general-purpose functionalities like fetching user information, downloading profile pictures, and some fun features like calculating IQ for fun.

## Features

- List organization members
- List open issues
- List pull requests
- Display help message with available commands
- Fetch user information
- Download profile pictures
- Fun features like calculating IQ, replace words in msgs, shuffle words etc
- Logging facilities for debugging and monitoring

## Commands

- `/start`: Display the help message
- `/issues`: Fetch and display open issues
- `/prs`: Fetch and display pull requests
- `/members`: Fetch and display organization members

## Installation

1. **Clone the Repository**

```bash
git clone https://github.com/ksauraj/fosscu-bot.git
cd fosscu-bot
```

2. **Set Up Telegram Bot**

- Create a new bot using [BotFather](https://core.telegram.org/bots#botfather) on Telegram and obtain the bot token.

3. **Set Up GitHub Token**

- Create a personal access token on GitHub with the necessary permissions to read organization members, repositories, issues, and pull requests.

4. **Run Initialization Script**

Execute the `init.sh` script to interactively set up the bot:

```bash
chmod +x init.sh bot.sh
./init.sh
```

Follow the prompts to input your Telegram bot token, GitHub token, GitHub organization name, and Telegram chat ID.

## Usage

1. **Run the Bot**

```bash
./bot.sh
```

2. **Interact with the Bot**

- Send `/start` to display the help message.
- Send `/issues` to fetch and display open issues.
- Send `/prs` to fetch and display pull requests.
- Send `/members` to fetch and display organization members.

## Directory Structure

- `init.sh`: Interactive setup script for the bot.
- `utils.sh`: Contains Telegram functions and utilities.
- `bot.sh`: Main script to run the bot.
- `bot.log`: File where log files are stored.

## Additional Features

- **User Information**: Fetch details about a user.
- **Profile Picture Download**: Download and send a user's profile picture.
- **Fun Features**: Fun feature to calculate IQ, Shuffle Words, Replace Words and others for entertainment purposes.

## Contributing

Feel free to submit issues and enhancement requests.

## Inspiration & Credits
`util.sh` was ported from [here](https://github.com/ksauraj/telegram-bash-bot/blob/master/util.sh) which was written by @ksauraj and @Hakimi0804 from scratch.

74 changes: 74 additions & 0 deletions bot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/bash

#Clear & Clean terminal before starting
clear
rm -f bot.log

# Source core utils
source .token.sh
source util.sh

# Source functions
source bot/all_replace.sh
source bot/calc.sh
source bot/choice.sh
source bot/github.sh
source bot/info.sh
source bot/iq.sh
source bot/log_dump.sh
source bot/neofetch.sh
source bot/pfp.sh
source bot/purge.sh
source bot/replace.sh
source bot/reset_log.sh
source bot/round.sh
source bot/start.sh
source bot/shuffle.sh
source bot/weath.sh
source bot/bot_util.sh
source bot/shell.sh

log -i tgbot "STARTING BOT"


# Defining constan# Initialize update
update_init

## While loop
while true; do
# Refresh stuff
update
[ "$RET_MSG_TEXT" ] && log -v tgbot "Message received: $RET_MSG_TEXT" | tee -a log
[ "$RET_MSG_TEXT" ] && log -v tgbot " -> Chat title: $RET_CHAT_TITLE" | tee -a log &
[ "$RET_MSG_TEXT" ] && log -v tgbot " -> Chat id: $RET_CHAT_ID" | tee -a log &
RET_LOWERED_MSG_TEXT=$(tr '[:upper:]' '[:lower:]' <<<"$RET_MSG_TEXT")

case $RET_LOWERED_MSG_TEXT in

'/start'*) start | tee -a log ;;
'.all_replace'*) all_replace | tee -a log ;;
'.calc'*) calc | tee -a log ;;
'.choice'*) choice | tee -a log ;;
'.iq'*) iq | tee -a log ;;
'.info'*) info | tee -a log ;;
'/issues'*) send_open_issues ;;
'.neofetch'*) neo_fetch | tee -a log ;;
'.pfp'*) pfp | tee -a log ;;
'.replace'*) replace | tee -a log ;;
'.round'*) round_msg | tee -a log;;
'.weath'*) weath | tee -a log ;;
'.log'*) log_dump ;;
'/members'*) send_members ;;
'/prs'*) send_open_pull_requests ;;
'.reset_log'*) reset_log ;;
'.shuffle'*) shuffle ;;
'.purge'*) purge ;;
'.restart'*) bot_util::restart ;;
'.update'*) bot_util::update ;;
'.shell'*) shell ;;
esac

unset RET_MSG_TEXT RET_REPLIED_MSG_ID
done


11 changes: 11 additions & 0 deletions bot/all_replace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

all_replace() {
TRIMMED=${RET_MSG_TEXT#.all_replace }
echo "${RET_REPLIED_MSG_TEXT}" > sed.txt
log -d all_replace "sed -i "s/${TRIMMED}/g" sed.txt"
sed -i "s/${TRIMMED}/g" sed.txt
text=$(cat sed.txt)
tg --replymsg "$RET_CHAT_ID" "$RET_REPLIED_MSG_ID" "${text}"
rm sed.txt
}
38 changes: 38 additions & 0 deletions bot/bot_util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Check if the bot was restarted when sourced
if [ -n "$BOT_RESTARTED" ]; then
log -i bot_util "Bot was restarted"
tg --editmsg "$RET_CHAT_ID" "$SENT_MSG_ID" "Bot restarted."
fi

bot_util::restart() {
tg --replymsg "$RET_CHAT_ID" "$RET_MSG_ID" "Restarting bot"
log -i bot_util "Restarting bot"
export BOT_RESTARTED=true
export RET_CHAT_ID
export SENT_MSG_ID
exec bash tgbot.sh
}

bot_util::update() {
tg --replymsg "$RET_CHAT_ID" "$RET_MSG_ID" "Updating bot"
log -i bot_util "Updating bot"
log -v bot_util "Adding safe directory to git"
git config --global --add safe.directory /app
log -v bot_util "Running git pull"
git pull || {
git update-ref -d HEAD
git pull
} || local bot_update_error=true

if [ "$bot_update_error" = true ]; then
tg --editmsg "$RET_CHAT_ID" "$SENT_MSG_ID" "Failed to update bot."
log -e bot_util "git pull failed"
else
tg --editmsg "$RET_CHAT_ID" "$SENT_MSG_ID" "Bot updated. It is recommended to restart the bot."
log -i "Bot updated"
log -d "git pull exited with success"
fi
}

12 changes: 12 additions & 0 deletions bot/calc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

calc() {
TRIMMED="${RET_MSG_TEXT#.calc}"
CALCED=$(echo "$TRIMMED" | bc -l 2>&1)
if ! echo "$CALCED" | grep -q 'syntax error'; then
ROUNDED=$(round "$CALCED" 2)
tg --replymsg "$RET_CHAT_ID" "$RET_MSG_ID" "$ROUNDED"
else
tg --replymsg "$RET_CHAT_ID" "$RET_MSG_ID" "Bruh, did you just entered nonsense, cuz bc ain't happy"
fi
}
7 changes: 7 additions & 0 deletions bot/extra/neofetch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

neo_fetch() {
tg --replymsg "$RET_CHAT_ID" "$RET_MSG_ID" "This may take a while depending upon host..."
NEOFETCH_OUTPUT=$(neofetch --stdout)
tg --editmsg "$RET_CHAT_ID" "$SENT_MSG_ID" "$NEOFETCH_OUTPUT"
}
13 changes: 13 additions & 0 deletions bot/extra/pfp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

pfp() {
if [ "${RET_REPLIED_MSGGER_ID}" != "null" ]; then
tg --getuserpfp "${RET_REPLIED_MSGGER_ID}"
tg --downloadfile "$FILE_ID" "pfp.jpg"
else
tg --getuserpfp "${MSGGER}"
tg --downloadfile "$FILE_ID" "pfp.jpg"
fi
tg --replyfile "$RET_CHAT_ID" "$RET_MSG_ID" "pfp.jpg"
rm pfp.jpg
}
Loading