-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistener.py
executable file
·108 lines (93 loc) · 3.7 KB
/
listener.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# Imports
import os
import signal
from loguru import logger
from telethon import TelegramClient, events
from configparser import ConfigParser
from twilio.rest import Client
# Remove Traceback on KeyboardInterrupt
signal.signal(signal.SIGINT, lambda x, y: os._exit(0))
# Prompts user for config input if file doesn't exist; otherwise, skips input and informs user.
cfg_file_path = "config.cfg"
# Check if the configuration file exists
if not os.path.exists(cfg_file_path):
logger.info("Configuration file does not exist: %s" % cfg_file_path)
# Define the template configuration file content with placeholders
cfg_template = """
[telethon]
id = {id}
hash = {hash}
channel = {channel}
include = {include}
exclude = {exclude}
[twilio]
SID = {sid}
token = {token}
twilioNo = {twilioNo}
send = {send}
message = {message}
"""
# Get user input for each configuration value
id = input("Your Telegram API id: ")
hash = input("Your Telegram API hash: ")
channel = input("Paste the URL of the channel you want to monitor: ")
include = input("Keyword you want to include in the recieved channel message: ")
exclude = input("Keyword you want to exclude in the recieved channel message: ")
sid = input("Your Twilio account SID: ")
token = input("Your Twilio account token: ")
twilioNo = input("Your Twilio account number: ")
send = input("The number you want to send the message to with the country code: ")
message = input("The message you want to send: ")
# Replace placeholders in the template with user input
cfg_content = cfg_template.format(id=id, hash=hash, channel=channel, include=include, exclude=exclude, sid=sid, token=token, twilioNo=twilioNo, send=send,message=message)
# Write the configuration content to a cfg file
with open("config.cfg", "w") as cfg_file:
cfg_file.write(cfg_content)
logger.info("Configuration saved to 'config.cfg'")
else:
logger.info("Configuration file already exists. Skipping input prompts.")
# Parse config from config.cfg
config = ConfigParser()
config.read('config.cfg')
logger.info("Reading configuration file 'config.cfg")
# Get user-specific variables from config.cfg
# Telegram
api_id = config.get('telethon', 'id')
api_hash = config.get('telethon', 'hash')
channel = config.get('telethon', 'channel')
# Keywords to include
keyword_include = config.get('telethon', 'include')
# Keywords to exclude
keyword_exclude = config.get('telethon', 'exclude')
# Twilio
account_SID = config.get('twilio', 'SID')
account_token = config.get('twilio', 'token')
twilioPh = config.get('twilio', 'twilioNo')
sendTo = config.get('twilio', 'send')
messageSent = config.get('twilio', 'message')
# Create a TelegramClient object
client = TelegramClient('my_account', api_id, api_hash)
# Create a TwilioClient object
twilioClient = Client(account_SID, account_token)
# Define an event handler for incoming messages in the channel
@client.on(events.NewMessage(chats=channel))
async def handle_new_message(event):
message_text = event.message.text.lower()
# Filter message accounting to keywords
if keyword_include in message_text and keyword_exclude not in message_text:
logger.info("Message recieved: %s" % event.message.text)
logger.info(event.message.text)
# Send a message to your phone number with the message recieved
twilioClient.messages.create(
body = messageSent + " " + event.message.text,
from_=twilioPh,
to=sendTo
)
logger.info("Message sent: %s" % event.message.text)
# Run the client
async def main():
await client.start()
await client.run_until_disconnected()
if __name__ == '__main__':
client.loop.run_until_complete(main())