Skip to content

Commit

Permalink
Merge pull request #23 from tm-a-t/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
vanutp authored Aug 8, 2022
2 parents 20a1b50 + 8b7b5d2 commit 6c6f75d
Show file tree
Hide file tree
Showing 54 changed files with 1,757 additions and 924 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__pycache__/
venv/
guide/site
61 changes: 61 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Build docker image
on:
workflow_call: {}

env:
DOCKERHUB_REPO: tgpy/tgpy

jobs:
Build-docker-image:
name: Build docker image
concurrency: release-docker
if: github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup QEMU
uses: docker/setup-qemu-action@v2
- name: Setup Buildx
uses: docker/setup-buildx-action@v2
- name: Setup Buildx caching
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Set release flag
if: github.ref == 'refs/heads/master'
run: sed -i "s/\(IS_DEV_BUILD *= *\).*/\1False/" tgpy/version.py

- name: Set branch tag
if: github.ref != 'refs/heads/master'
run: |
BRANCH_TAG=$DOCKERHUB_REPO:$(git rev-parse --abbrev-ref HEAD)
echo "BRANCH_TAG=$BRANCH_TAG" >> $GITHUB_ENV
echo "IMAGE_TAGS=$IMAGE_TAGS -t $BRANCH_TAG" >> $GITHUB_ENV
- name: Set latest tag
if: github.ref == 'refs/heads/master'
run: |
BRANCH_TAG=$DOCKERHUB_REPO:latest
echo "BRANCH_TAG=$BRANCH_TAG" >> $GITHUB_ENV
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Build and push image
run: |
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t $DOCKERHUB_REPO:$(git rev-parse --short HEAD) \
-t $BRANCH_TAG \
--cache-from "type=local,src=/tmp/.buildx-cache" \
--cache-to "type=local,dest=/tmp/.buildx-cache-new,mode=max" \
--push .
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
20 changes: 20 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ name: Lint & release project
on:
push:
paths:
- 'pyproject.toml'
- 'poetry.lock'
- 'tgpy/**'
- '.github/workflows/main.yml'
- '.github/workflows/docker.yml'
pull_request:
paths:
- 'pyproject.toml'
- 'poetry.lock'
- 'tgpy/**'
- '.github/workflows/main.yml'
- '.github/workflows/docker.yml'

jobs:
Lint:
Expand Down Expand Up @@ -94,3 +100,17 @@ jobs:
export REPOSITORY_USERNAME="__token__"
export REPOSITORY_PASSWORD="${PYPI_TOKEN}"
python -m semantic_release publish -D commit_author="github-actions <[email protected]>"
Build-dev-docker:
name: Build dev docker image
if: github.event_name == 'push' && github.ref != 'refs/heads/master'
uses: ./.github/workflows/docker.yml
secrets: inherit

Build-release-docker:
name: Build release docker image
needs: Release
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
uses: ./.github/workflows/docker.yml
secrets: inherit

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ dist/
data/
config.py
config.yaml

guide/site
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# syntax=docker/dockerfile:1.3
FROM python:3.10-alpine as base
WORKDIR /app

FROM base as builder
RUN apk add --no-cache gcc musl-dev libffi-dev git rust cargo

ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
POETRY_VERSION=1.1.13

RUN --mount=type=cache,target=/root/.cache/pip \
pip install poetry==$POETRY_VERSION
RUN python -m venv /venv

COPY pyproject.toml poetry.lock ./
RUN --mount=type=cache,target=/root/.cache/pip \
poetry export -f requirements.txt | /venv/bin/pip install -r /dev/stdin

COPY . .
RUN git status
RUN sed -i "s/\(COMMIT_HASH *= *\).*/\1'$(git rev-parse HEAD)'/" tgpy/version.py

FROM base as runner
COPY --from=builder /venv /venv
COPY --from=builder /app/tgpy tgpy

ENV TGPY_DATA=/data
VOLUME /data

ENTRYPOINT ["/venv/bin/python", "-m", "tgpy"]
83 changes: 23 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,88 +1,51 @@
# TGPy

### Run Python code right in your Telegram messages
_Run Python code right in your Telegram messages_

TGPy is a tool for evaluating expressions and Telegram API scripts, built on top of [Telethon](https://github.com/LonamiWebs/Telethon).
![PyPI](https://img.shields.io/pypi/v/tgpy)
![Docker Image Version (latest semver)](https://img.shields.io/docker/v/tgpy/tgpy?label=docker&sort=semver)
![Open issues](https://img.shields.io/github/issues-raw/tm-a-t/TGPy)
![Docs](https://img.shields.io/website?label=docs&url=https%3A%2F%2Ftgpy.tmat.me)
<!-- ![PyPI - Downloads](https://img.shields.io/pypi/dm/tgpy) -->

- Do Python calculations in dialogs
- Interact with your messages and chats
- Automate sending messages and more

**Learn basics:** https://tgpy.tmat.me/basics/
TGPy is a tool for running Python expressions and Telegram API scripts, built on top of [Telethon](https://github.com/LonamiWebs/Telethon).

- Do Python calculations in dialogs
- Use code to send messages, save files, analyze chats and more
- Set up functions to automate your Telegram actions

## Getting started

Python 3.9+ required. Host TGPy and connect it to your Telegram account:
Python 3.9+ required. Install TGPy and connect it to your Telegram account:

```shell
> pip install tgpy
> tgpy
```

You’re ready now. Send Python code to any chat to run it. Change your message to change the result.

<img alt="" src="https://raw.githubusercontent.com/tm-a-t/TGPy/master/readme_assets/example.gif" width="540">

## Examples

Send any of these examples to any chat to evaluate:

🐍 Do Python calculations

```python
for i in range(5):
print(i)
```

⏳ Delete the current message in 5 seconds
You’re ready now. Send Python code to any chat, and it will run. Change your message to change the result.

```python
import asyncio
Details on installation: http://tgpy.tmat.me/installation/

await asyncio.sleep(5)
await msg.delete()
```
https://user-images.githubusercontent.com/38432588/181266550-c4640ff1-71f2-4868-ab83-6ea3690c01b6.mp4

↪️ Forward the message you replied to to another chat
## New TGPy docs

```python
orig.forward_to('Chat title')
```
**[Basics Guide:](http://tgpy.tmat.me/basics/code/)** All you need to know to start using TGPy.

🖼 Send all chat profile photos to the same chat

```python
photos = await client.get_profile_photos(msg.chat)
msg.reply(file=photos)
```
**[Extensibility Guide:](http://tgpy.tmat.me/extensibility/context/)** Special features for advanced usage.

🔖 Define a function which forwards messages to Saved Messages with reply
**[Reference:](http://tgpy.tmat.me/reference/builtins/)** List of TGPy objects and settings.

```python
def save():
message = ctx.msg
original = await message.get_reply_message()
await original.forward_to('me')
return 'Saved!'
```
## Inspiration

🗑 Define a function which deletes messages with reply

```python
async def delete():
message = ctx.msg
original = await message.get_reply_message()
await original.delete()
await message.delete()
```

## [TGPy Guide](https://tgpy.tmat.me/)
TGPy is inspired by [FTG](https://gitlab.com/friendly-telegram/friendly-telegram) and similar userbots. However, the key concept is different: TGPy is totally based on usage of code in Telegram rather than plugging extra modules. This leads both to convenience of single-use scripts and reusage flexibility.

## Credits

- Thanks to [penn5](https://github.com/penn5) for [meval](https://github.com/penn5/meval)
- Thanks to [Lonami](https://github.com/LonamiWebs) for [Telethon](https://github.com/LonamiWebs/Telethon)
TGPy is built on [Telethon](https://github.com/LonamiWebs/Telethon), which allows to integrate Telegram features in Python code.

Basic code transformation (such as auto-return of values) is based on [meval](https://github.com/penn5/meval).

## License

Expand Down
14 changes: 0 additions & 14 deletions guide/docs/api.md

This file was deleted.

Binary file removed guide/docs/assets/example.gif
Binary file not shown.
Binary file added guide/docs/assets/example.mp4
Binary file not shown.
118 changes: 0 additions & 118 deletions guide/docs/basics.md

This file was deleted.

Loading

0 comments on commit 6c6f75d

Please sign in to comment.