Skip to content

Instantly share code, notes, and snippets.

@blueset
Last active May 14, 2019 15:49
Show Gist options
  • Save blueset/ca6dc7ec5cc7a41656ed8944e374b60f to your computer and use it in GitHub Desktop.
Save blueset/ca6dc7ec5cc7a41656ed8944e374b60f to your computer and use it in GitHub Desktop.

Revisions

  1. blueset revised this gist Feb 1, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion documents.patch
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ index a58e70a..8395d6d 100644
    + assert (json_object_set (res, "size", json_integer (M->document->size)) >= 0);
    +
    + if (M->caption) {
    + assert (json_object_set (res, "caption", json_string (M->document->caption)) >= 0);
    + assert (json_object_set (res, "caption", json_string (M->caption)) >= 0);
    + }
    + }
    break;
  2. blueset revised this gist Jan 26, 2017. 2 changed files with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,5 @@
    3. `pip3 install pytg`
    4. Modify `main.py` and change `tgdir` to the correct path.
    5. Log in your tg-cli
    6. `python3 main.py`
    6. `nohup ./bin/telegram-cli --json -R -W -P 4458 --permanent-peer-ids --permanent-peer-ids > /dev/null &`
    7. `nohup python3 kagami.py > /dev/null &`
    File renamed without changes.
  3. blueset revised this gist Jan 26, 2017. 1 changed file with 24 additions and 11 deletions.
    35 changes: 24 additions & 11 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,38 @@
    from pytg import Telegram
    from pytg import Telegram from pytg.receiver import Receiver
    from pytg.sender import Sender
    from pytg.utils import coroutine
    import time
    import threading

    tgdir = "/root/tg/"
    t = Telegram(telegram=tgdir + "bin/telegram-cli",
    pubkey_file=tgdir + "tg-server.pub")
    #t = Telegram(telegram=tgdir + "bin/telegram-cli",
    # pubkey_file=tgdir + "tg-server.pub")

    def rm_msg(sender, msg_id):
    def rm_msg(sender, msg_id, msg):
    print("sticker!", msg_id)
    f = msg.get('sender', {'first_name': ''})
    warn = f['first_name']
    warn += " " + f.get('last_name', '') if f.get('last_name', '') else ''
    warn += " (@%s)" % f.get('username', '') if f.get('username', '') else ''
    warn += ', no stickers. Warn issued.'
    sender.reply(msg_id, warn)
    print(warn)
    time.sleep(60)
    sender.raw("remove_msg %s" % msg_id)
    sender.raw("delete_msg %s" % msg_id)
    print("removed!", msg_id)

    @coroutine
    def loop(sender):
    try:
    while True:
    msg = (yield)
    if msg['flags'] == 256 && msg.get('media', {}).get('caption', "") == "sticker.webp":
    threading.Thread(target=rm_msg, args=(sender, msg['id'])).start()
    except:
    pass
    if msg.get('flags', 0) == 256 and msg.get('media', {}).get('caption', '') == "sticker.webp":
    threading.Thread(target=rm_msg, args=(sender, msg['id'], msg)).start()
    except Exception as e:
    print(e)

    r.start()
    r.message(loop(t.sender))
    receiver = Receiver("127.0.0.1", 4458)
    sender = Sender("127.0.0.1", 4458)

    receiver.start()
    receiver.message(loop(sender))
  4. blueset created this gist Jan 25, 2017.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    1. Clone https://github.com/vysheng/tg/, and install all necessary dependencies
    2. Apply `documents.patch`, then `./configure && make`
    3. `pip3 install pytg`
    4. Modify `main.py` and change `tgdir` to the correct path.
    5. Log in your tg-cli
    6. `python3 main.py`
    46 changes: 46 additions & 0 deletions documents.patch
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    diff --git a/json-tg.c b/json-tg.c
    index a58e70a..8395d6d 100644
    --- a/json-tg.c
    +++ b/json-tg.c
    @@ -233,6 +233,41 @@ json_t *json_pack_media (struct tgl_message_media *M) {
    case tgl_message_media_video:
    case tgl_message_media_document_encr:
    assert (json_object_set (res, "type", json_string ("document")) >= 0);
    + if (M->type == tgl_message_media_document){
    + assert (M->document);
    + if (M->document->flags & TGLDF_IMAGE) {
    + assert (json_object_set (res, "media", json_string ("image")) >= 0);
    + } else if (M->document->flags & TGLDF_AUDIO) {
    + assert (json_object_set (res, "media", json_string ("audio")) >= 0);
    + } else if (M->document->flags & TGLDF_VIDEO) {
    + assert (json_object_set (res, "media", json_string ("video")) >= 0);
    + } else if (M->document->flags & TGLDF_STICKER) {
    + assert (json_object_set (res, "media", json_string ("sticker")) >= 0);
    + } else {
    + assert (json_object_set (res, "media", json_string ("document")) >= 0);
    + }
    + if (M->document->caption && strlen (M->document->caption)) {
    + assert (json_object_set (res, "dcaption", json_string (M->document->caption)) >= 0);
    + }
    +
    + if (M->document->mime_type) {
    + assert (json_object_set (res, "mime", json_string (M->document->mime_type)) >= 0);
    + }
    +
    + if (M->document->w && M->document->h) {
    + assert (json_object_set (res, "width", json_integer (M->document->w)) >= 0);
    + assert (json_object_set (res, "height", json_integer (M->document->h)) >= 0);
    + }
    +
    + if (M->document->duration) {
    + assert (json_object_set (res, "duration", json_integer (M->document->duration)) >= 0);
    + }
    + assert (json_object_set (res, "size", json_integer (M->document->size)) >= 0);
    +
    + if (M->caption) {
    + assert (json_object_set (res, "caption", json_string (M->document->caption)) >= 0);
    + }
    + }
    break;
    case tgl_message_media_unsupported:
    assert (json_object_set (res, "type", json_string ("unsupported")) >= 0);
    25 changes: 25 additions & 0 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    from pytg import Telegram
    from pytg.utils import coroutine
    import time
    import threading

    tgdir = "/root/tg/"
    t = Telegram(telegram=tgdir + "bin/telegram-cli",
    pubkey_file=tgdir + "tg-server.pub")

    def rm_msg(sender, msg_id):
    time.sleep(60)
    sender.raw("remove_msg %s" % msg_id)

    @coroutine
    def loop(sender):
    try:
    while True:
    msg = (yield)
    if msg['flags'] == 256 && msg.get('media', {}).get('caption', "") == "sticker.webp":
    threading.Thread(target=rm_msg, args=(sender, msg['id'])).start()
    except:
    pass

    r.start()
    r.message(loop(t.sender))
    1 change: 1 addition & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    pytg