Skip to content

Commit

Permalink
Merge branch 'athphane:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
azuwlaa authored Jul 24, 2024
2 parents 2c167d8 + c9788c0 commit 1eb5bf0
Show file tree
Hide file tree
Showing 18 changed files with 340 additions and 42 deletions.
30 changes: 30 additions & 0 deletions .build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Stage 1: Build
FROM python:3.11.5-slim-bullseye AS build

WORKDIR /root/userbot

COPY . .

ENV DEBIAN_FRONTEND=noninteractive

RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/root/.cache/pip \
apt-get update && \
apt-get install -y gcc build-essential --no-install-recommends && \
pip install -r requirements.txt && \
apt-get autoremove -y gcc build-essential && \
apt-get clean

# Stage 2: Run
FROM python:3.11.5-slim-bullseye

LABEL org.opencontainers.image.source https://github.com/athphane/userbot
LABEL org.opencontainers.image.description "Telegram Userbot Container Image"
LABEL org.opencontainers.image.licenses GPL-3.0

WORKDIR /root/userbot

COPY --from=build /root/userbot .
COPY --from=build /usr/local /usr/local

CMD python -m userbot
13 changes: 13 additions & 0 deletions .build/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.5'
services:
#########################
userbot-x86: &userbot-arm
build:
context: ../
dockerfile: .build/Dockerfile
hostname: userbot
image: ghcr.io/athphane/userbot
platform: linux/amd64/v3
# userbot-arm:
# <<: *userbot-arm
# platform: linux/arm64
11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
venv/
.idea/
.env
*.session
*.session-journal
__pycache__/
unknown_errors.txt
file_ids.txt
.cache-*
.git
config/*
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Docker Build

on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Build and push Docker image
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
run: |
docker-compose -f .build/docker-compose.yml build
docker login ghcr.io -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
docker-compose -f .build/docker-compose.yml push
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ venv/
*.session
*.session-journal
__pycache__/
userbot.ini
config/userbot.ini
unknown_errors.txt
file_ids.txt
.cache-*
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,27 @@ source venv/bin/activate
pip install -r requirements.txt
python -m userbot
```
*Using docker*
- Save the following content as `docker-compose.yml`
```yml
version: '3.5'
services:
userbot:
hostname: userbot
volumes:
- ./config:/root/userbot/config
image: ghcr.io/athphane/userbot:latest

```
- First run:
```bash
docker compose run --rm userbot python -m userbot
```
- Configure userbot and stop container **CTRL+C**
- Run the container again de-attached:
```bash
docker compose up -d
```
## Spotify integration
To get the Spotify commands working on the Userbot, you need to get a CLIENT_ID and CLIENT_SECRET
form the [Spotify Developer Portal]('https://developer.spotify.com/dashboard/applications) and add
Expand All @@ -72,6 +92,17 @@ project root of the server that will be running your userbot. This is a one time
* `.restart` command do not work on Termux as [psutils is not supported on Android](https://github.com/giampaolo/psutil/issues/913). \
CTRL+C, run `git pull` and `python -m userbot` to update bot on termux.

## Star History

<a href="https://star-history.com/#athphane/userbot&Date">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=athphane/userbot&type=Date&theme=dark" />
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=athphane/userbot&type=Date" />
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=athphane/userbot&type=Date" />
</picture>
</a>


## Credits, and Thanks to
* [Dan](https://t.me/haskell) for his [Pyrogram Library](https://github.com/pyrogram/pyrogram)
* [Colin Shark](https://t.me/ColinShark) for his [PyroBot](https://git.colinshark.de/PyroBot/PyroBot) which helped with
Expand Down
Empty file added config/.gitkeep
Empty file.
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.5'
services:
#########################
userbot:
hostname: userbot
volumes:
- ./:/root/userbot
image: ghcr.io/athphane/userbot
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pyaes==1.6.1
pygit==0.1
pymongo==4.3.2
pyparsing==3.0.9
Pyrogram==2.0.59
Pyrogram==2.0.106
PySocks==1.7.1
pytz==2022.5
pytz-deprecation-shim==0.1.0.post0
Expand Down
2 changes: 1 addition & 1 deletion spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import spotipy
import spotipy.util as util

config_file = 'userbot.ini'
config_file = 'config/userbot.ini'

config = ConfigParser()
config.read(config_file)
Expand Down
20 changes: 19 additions & 1 deletion userbot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ast
import logging
import os
import sys
from configparser import ConfigParser
from datetime import datetime
from logging.handlers import TimedRotatingFileHandler
Expand All @@ -11,6 +12,23 @@
if not os.path.exists('logs'):
os.makedirs('logs')

# Create config folder if it is not there. Needed for config.
if not os.path.exists('config'):
os.makedirs('config')

# Move the userbot.ini file from root to config folder if it is not there.
if os.path.exists('userbot.ini'):
# If 'userbot.ini' exists, move it to 'config/userbot.ini'
os.rename('userbot.ini', 'config/userbot.ini')
elif not os.path.exists('config/userbot.ini'):
# If 'config/userbot.ini' also does not exist, print the message and exit the program
print("Please create and configure config/userbot.ini and re-run the setup")
sys.exit(1)

# Move the userbot.session file from root to config folder if it is not there.
if not os.path.exists('config/userbot.session') and os.path.exists('userbot.session'):
os.rename('userbot.session', 'config/userbot.session')

# Logging at the start to catch everything
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
Expand All @@ -35,7 +53,7 @@
UserBot = UserBot(__version__)

# Read from config file
config_file = 'userbot.ini'
config_file = 'config/userbot.ini'
config = ConfigParser()
config.read(config_file)

Expand Down
109 changes: 109 additions & 0 deletions userbot/plugins/morse_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import asyncio

from pyrogram import filters
from pyrogram.enums import ParseMode
from pyrogram.types import Message

from userbot import UserBot
from userbot.plugins.help import add_command_help

# Dictionary representing the morse code chart
MORSE_CODE_DICT = {'A': '.-', 'B': '-...',
'C': '-.-.', 'D': '-..', 'E': '.',
'F': '..-.', 'G': '--.', 'H': '....',
'I': '..', 'J': '.---', 'K': '-.-',
'L': '.-..', 'M': '--', 'N': '-.',
'O': '---', 'P': '.--.', 'Q': '--.-',
'R': '.-.', 'S': '...', 'T': '-',
'U': '..-', 'V': '...-', 'W': '.--',
'X': '-..-', 'Y': '-.--', 'Z': '--..',
'1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....',
'7': '--...', '8': '---..', '9': '----.',
'0': '-----', ', ': '--..--', '.': '.-.-.-',
'?': '..--..', '/': '-..-.', '-': '-....-',
'(': '-.--.', ')': '-.--.-', ' ': '/'}


@UserBot.on_message(filters.command("morse", ".") & filters.me)
async def morse_encrypt(bot: UserBot, message: Message):
cmd = message.command

def encrypt(input_string):
cipher = ''
for letter in input_string:
if letter != ' ':
cipher += MORSE_CODE_DICT[letter] + ' '
else:
cipher += ' '

return cipher

main_text = ""
if len(cmd) > 1:
main_text = " ".join(cmd[1:])
elif message.reply_to_message and len(cmd) == 1:
main_text = message.reply_to_message.text
elif not message.reply_to_message and len(cmd) == 1:
await message.edit("I need something to encrypt")
await asyncio.sleep(2)
await message.delete()
return

input_str = main_text
if not input_str:
await message.edit("`give me something to encrypt`")
return

await message.edit(encrypt(input_str.upper()), parse_mode=ParseMode.DISABLED)


@UserBot.on_message(filters.command("morsed", ".") & filters.me)
async def morse_decrypt(bot: UserBot, message: Message):
cmd = message.command

def decrypt(input_string):
input_string += ' '
decipher = ''
citext = ''
for letter in input_string:
if letter != ' ':
i = 0
citext += letter
else:
i += 1
if i == 2:
decipher += ' '
else:
decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT.values()).index(citext)]
citext = ''

return decipher

main_text = ""
if len(cmd) > 1:
main_text = " ".join(cmd[1:])
elif message.reply_to_message and len(cmd) == 1:
main_text = message.reply_to_message.text
elif not message.reply_to_message and len(cmd) == 1:
await message.edit("I need something to decrypt")
await asyncio.sleep(2)
await message.delete()
return

input_str = main_text
if not input_str:
await message.edit("`give me something to decrypt`")
return

await message.edit(decrypt(input_str).lower(), parse_mode=ParseMode.DISABLED)


# Command help section
add_command_help(
"morse",
[
[".morse", "Encrypt the input text into morse code"],
[".morsed", "Decrypt morse code to plain text"],
],
)
24 changes: 24 additions & 0 deletions userbot/plugins/spotify.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import asyncio

from pyrogram import emoji, filters
from pyrogram.enums import ParseMode
from pyrogram.types import Message

from userbot import ALLOWED_USERS, UserBot
Expand All @@ -25,6 +28,27 @@ async def now_playing(bot: UserBot, message: Message):
await message.edit(f'{emoji.MUSICAL_NOTE} Currently Playing: <a href="{link}">{song}</a>')


@UserBot.on_message(filters.command(["npd"], ".") & (filters.me | filters.user(ALLOWED_USERS)))
async def download_now_playing_song(bot: UserBot, message: Message):
current_track = await spotify.now_playing()

if not current_track:
await message.edit("I am not playing any music right now!")
return

if current_track == "API details not set":
await message.edit("API details not set. Please read the README!")
return

track = current_track['item']
link = track['external_urls']['spotify']

await asyncio.gather(
bot.send_message('@deezermusicbot', link, parse_mode=ParseMode.DISABLED),
message.delete()
)


@UserBot.on_message(
filters.command(["sdev", "sdevices", "spotifydevices", "sd"], ".") & (filters.me | filters.user(ALLOWED_USERS)))
async def list_devices(bot: UserBot, message: Message):
Expand Down
32 changes: 0 additions & 32 deletions userbot/plugins/subreddit_link.py

This file was deleted.

Loading

0 comments on commit 1eb5bf0

Please sign in to comment.