├── .env ├── README.md ├── LICENSE ├── .gitignore ├── main.py └── quizzes.json /.env: -------------------------------------------------------------------------------- 1 | API_ID = YOUR API ID 2 | API_HASH = 'YOUR API HASH' 3 | DEBUG_MODE = True -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # telegram auto reply 2 | Python script using telethon for auto replying telegram message 3 | 4 | How to use 5 | 6 | 1. Get Telegram API Key
7 | got to https://my.telegram.org and go to API development tools 8 | 9 | 2. Install Python
10 | https://www.python.org/downloads/ 11 | 12 | 3. Install telethon
13 | https://docs.telethon.dev/en/latest/basic/installation.html 14 | 15 | 4. Open .env and setup your environtment 16 | notes: 17 | if debug mode set to true, script will only response to message from self 18 |

19 | API_ID = YOUR API ID
20 | API_HASH = 'YOUR API HASH'
21 | DEBUG_MODE = false
22 | 
23 | 24 | 5. Run main.py 25 | 26 | 6. Enjoy! 🍻 27 | 28 | Example 29 | 30 | ![Example](https://pbs.twimg.com/media/EDNXtmfUEAAY2u8?format=jpg&name=4096x4096) 31 | 32 | inspired by 33 | https://gist.github.com/yi-jiayu/acc31fbad5a25f746430428ce4d62c28 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ibrahim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # A simple script to print some messages. 3 | import time 4 | import re 5 | import json 6 | import random 7 | import os 8 | from pprint import pprint 9 | 10 | from telethon import TelegramClient, events, utils 11 | from dotenv import load_dotenv 12 | 13 | load_dotenv() # get .env variable 14 | 15 | session = os.environ.get('TG_SESSION', 'printer') 16 | api_id = os.getenv("API_ID") 17 | api_hash = os.getenv("API_HASH") 18 | debug_mode = os.getenv("DEBUG_MODE").upper() == "TRUE" 19 | 20 | proxy = None # https://github.com/Anorov/PySocks 21 | 22 | # Create and start the client so we can make requests (we don't here) 23 | client = TelegramClient(session, api_id, api_hash, proxy=proxy).start() 24 | 25 | # create a sender list to check if user already send private message or mention 26 | senderList = [] 27 | 28 | #read json file and prepare quiz to send later 29 | with open('quizzes.json') as json_file: 30 | quizzes = json.load(json_file) 31 | 32 | @client.on(events.NewMessage) 33 | async def handle_new_message(event): 34 | 35 | me = await client.get_me().username 36 | from_ = await event.client.get_entity(event.from_id) # this lookup will be cached by telethon 37 | to_ = await event.client.get_entity(event.message.to_id) 38 | 39 | needToProceed = from_.is_self if debug_mode else not from_.is_self and (event.is_private or re.search("@"+me.username,event.raw_text)) 40 | if needToProceed: # only auto-reply to private chats: # only auto-reply to private chats 41 | if not from_.bot and event: # don't auto-reply to bots 42 | print(time.asctime(), '-', event.message) # optionally log time and message 43 | time.sleep(1) # pause for 1 second to rate-limit automatic replies 44 | message = "" 45 | senderList.append(to_.id) 46 | if senderList.count(to_.id) < 2: 47 | message = f"""**AUTO REPLY** 48 | \nHi @{from_.username}, 49 | \n\nMohon maaf boss saya sedang offline, mohon tunggu sebentar. 50 | \nSilahkan lihat-lihat [imacakes](https://www.instagram.com/ima_cake_cirebon) dulu untuk cuci mata. 51 | \n\n**AUTO REPLY**""" 52 | elif senderList.count(to_.id) < 3: 53 | message = f"""**AUTO REPLY** 54 | \nMohon bersabar @{from_.username}, boss saya masih offline 😒""" 55 | elif senderList.count(to_.id) < 4: 56 | message = f"""**AUTO REPLY** 57 | \n@{from_.username} Tolong bersabar yaa 😅""" 58 | else: 59 | random_number = random.randint(0,len(quizzes) - 1) 60 | question = quizzes[random_number]['question'] 61 | answer = quizzes[random_number]['answer'] 62 | message = f"""**AUTO REPLY** 63 | \n @{from_.username}, Main tebak-tebakan aja yuk 😁 64 | \n {question} 65 | \n {answer} 66 | \n """ 67 | 68 | if message != "": 69 | await event.reply(message) 70 | 71 | client.start() 72 | client.run_until_disconnected() 73 | -------------------------------------------------------------------------------- /quizzes.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "question":"Minuman apa yang islami?", 4 | "answer":"Nutrisyar'i" 5 | }, 6 | { 7 | "question":"Pemain bola apa yang beratnya 3kg?", 8 | "answer":"Bambang tabung gas" 9 | }, 10 | { 11 | "question":"Air putih yang bikin joged?", 12 | "answer":"Ades pacito" 13 | }, 14 | { 15 | "question":"Siapa nama cicak?", 16 | "answer":"Tatang , *nyanyi tatang seekor nyamuk" 17 | }, 18 | { 19 | "question":"Minuman yang paling sopan?", 20 | "answer":"MariMas" 21 | }, 22 | { 23 | "question":"Minuman yang suka berbagi?", 24 | "answer":"NutriShareIt" 25 | }, 26 | { 27 | "question":"Kacang yang gak pernah ontime?", 28 | "answer":"Kacang keDelay" 29 | }, 30 | { 31 | "question":"Setan yang S3 Marketing?", 32 | "answer":"Hantu pemasaran" 33 | }, 34 | { 35 | "question":"Penyanyi luar negeri yang asli Cirebon?", 36 | "answer":"Justin 'beber" 37 | }, 38 | { 39 | "question":"Wakil presiden yang sering nonton streming?", 40 | "answer":"Muhammad Youtube Kalla" 41 | }, 42 | { 43 | "question":"Terjemahkan ke dalam bahasa jepang “tidak ada uang?", 44 | "answer":"Sakurata takupusing" 45 | }, 46 | { 47 | "question":"Atlet apa yang beratnya cuman 3 kg?", 48 | "answer":"Bambang Tabung Gas" 49 | }, 50 | { 51 | "question":"Artis yang ditunggu-tunggu di bulan Ramadhan?", 52 | "answer":"Afgan Maghrib" 53 | }, 54 | { 55 | "question":"Presiden presiden apa yang sering ngasih promo?", 56 | "answer":"J.co wi" 57 | }, 58 | { 59 | "question":"Penyanyi apa yang hobby mikir?", 60 | "answer":"Ayu Thingking" 61 | }, 62 | { 63 | "question":"Kepalanya merah, jalannya mundur, tapi bukan undur-undur?", 64 | "answer":"Obat nyamuk bakar" 65 | }, 66 | { 67 | "question":"Kutu-kutu apa yang paling mengerikan?", 68 | "answer":"kutu-kan" 69 | }, 70 | { 71 | "question":"Bahasa mandarinnya lantai basah?", 72 | "answer":"Lhi Chin!" 73 | }, 74 | { 75 | "question":"Sebutkan 4 nama buah dalam satu detik?", 76 | "answer":"Rujak" 77 | }, 78 | { 79 | "question":"Cicak Cicak apa yang bikin mati?", 80 | "answer":"Cicak Napas" 81 | }, 82 | { 83 | "question":"Kenapa zombie nyerang barengan?", 84 | "answer":"Kalo sendiri namanya Zomblo" 85 | }, 86 | { 87 | "question":"Jus apa yang gak pernah manis?", 88 | "answer":"Jus a friend with u" 89 | }, 90 | { 91 | "question" : "Paus paus apa yang suka ke masjid?", 92 | "answer" : "PaUstad" 93 | }, 94 | { 95 | "question" : "Siapa penyanyi yang suka marah-marah?", 96 | "answer" : "Inul Daratinggi" 97 | }, 98 | { 99 | "question" : "Nengok kanan kiri tapi gak nyebrang2?", 100 | "answer" : "Kipas angin" 101 | }, 102 | { 103 | "question" : "Ban apa yang udaranya dingin?", 104 | "answer" : "Bandung" 105 | }, 106 | { 107 | "question" : "Naiknya cepet, turunya lambat banget?", 108 | "answer" : "Ingus" 109 | }, 110 | { 111 | "question" : "Sayur apa yang selalu dingin?", 112 | "answer" : "Sayur COLD" 113 | }, 114 | { 115 | "question" : "Hewan apa yang gak pernah salah?", 116 | "answer" : "Kucing ga Wrong" 117 | }, 118 | { 119 | "question" : "Artis yang emosian?", 120 | "answer" : "Anjasmaramara" 121 | }, 122 | { 123 | "question" : "Buah apa yang gak tau malu?", 124 | "answer" : "O raisin" 125 | }, 126 | { 127 | "question" : "Benda kecil apa yang bisa ngeluarin orang?", 128 | "answer" : "Bel rumah" 129 | }, 130 | { 131 | "question" : "Lemari apa yg bisa masuk kantong?", 132 | "answer" : "Lemaribuan" 133 | }, 134 | { 135 | "question" : "Belajar bahasa Mandarin, lantai basah artinya apa?", 136 | "answer" : "Lhi Chin!" 137 | }, 138 | { 139 | "question" : "Kenapa orang naik taksi tidak bayar uang dahulu?", 140 | "answer" : "Karena uang dahulu sudah tidak laku" 141 | } 142 | ] 143 | --------------------------------------------------------------------------------