forked from cyberjunky/3commas-cyber-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_contract.py
executable file
·167 lines (142 loc) · 4.98 KB
/
telegram_contract.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
"""Cyberjunky's 3Commas bot helpers."""
import argparse
import configparser
import asyncio
import os
import sys
import time
import json
import re
from pathlib import Path
from telethon import TelegramClient, events
from telethon.errors.rpcerrorlist import ChatAdminRequiredError
from helpers.logging import Logger, NotificationHandler
def load_config():
"""Create default or load existing config file."""
cfg = configparser.ConfigParser()
if cfg.read(f"{datadir}/{program}.ini"):
return cfg
cfg["settings"] = {
"timezone": "Europe/Amsterdam",
"debug": True,
"logrotate": 7,
"tgram-phone-number": "Your Telegram Phone number",
"tgram-channel": "Telegram Channel name to watch",
"tgram-api-id": "Your Telegram API ID",
"tgram-api-hash": "Your Telegram API Hash",
"notifications": False,
"notify-urls": ["notify-url1"],
"blacklist-msg": ["honeypot", "risk"],
"blacklist-line": ["Owner"],
}
with open(f"{datadir}/{program}.ini", "w") as cfgfile:
cfg.write(cfgfile)
return None
# Start application
program = Path(__file__).stem
# Parse and interpret options.
parser = argparse.ArgumentParser(description="Cyberjunky's Tamer bot helper.")
parser.add_argument(
"-d", "--datadir", help="directory to use for config and logs files", type=str
)
args = parser.parse_args()
if args.datadir:
datadir = args.datadir
else:
datadir = os.getcwd()
# Create or load configuration file
config = load_config()
if not config:
# Initialise temp logging
logger = Logger(datadir, program, None, 7, False, False)
logger.info(
f"Created example config file '{datadir}/{program}.ini', edit it and restart the program"
)
sys.exit(0)
else:
# Handle timezone
if hasattr(time, "tzset"):
os.environ["TZ"] = config.get(
"settings", "timezone", fallback="Europe/Amsterdam"
)
time.tzset()
# Init notification handler
notification = NotificationHandler(
program,
config.getboolean("settings", "notifications"),
config.get("settings", "notify-urls"),
)
# Initialise logging
logger = Logger(
datadir,
program,
notification,
int(config.get("settings", "logrotate", fallback=7)),
config.getboolean("settings", "debug"),
config.getboolean("settings", "notifications"),
)
logger.info(f"Loaded configuration from '{datadir}/{program}.ini'")
# Configuration settings
monitor = config.get("settings", "tgram-channel")
blacklist_msg = config.get("settings", "blacklist-msg")
blacklist_line = config.get("settings", "blacklist-line")
# Setup the Telegram Client
client = TelegramClient(
f"{datadir}/{program}",
config.get("settings", "tgram-api-id"),
config.get("settings", "tgram-api-hash"),
)
client.start(config.get("settings", "tgram-phone-number"))
async def setup():
"""Setup."""
user = await client.get_me()
logger.info("Started relaying as {}".format(user.first_name))
await client.get_dialogs()
def blacklist(words, line, whole=False):
"""Check for string in line."""
if whole:
for ln in line:
for word in json.loads(words.replace("'", '"')):
if word in ln:
return word
else:
for word in json.loads(words.replace("'", '"')):
if word in line:
return word
return None
@client.on(events.NewMessage(chats=monitor))
async def my_event_handler(event):
"""Parse messages."""
contract = ""
if event.message.message:
lines = event.message.message.split("\n")
found = blacklist(blacklist_msg, lines, True)
if found:
logger.info(f"Found blacklisted word '{found}' in telegram message, skipping.")
else:
for line in lines:
temp=re.findall(r'\b[0x]\w+', line)
if len(temp):
found = blacklist(blacklist_line, line)
if found:
logger.info(f"Found blacklisted word '{found}' in contract line, skipping.")
continue
contract = temp[0]
logger.info(f"Found contract: '{contract}' in telegram message")
try:
logger.info(f"Sent contract command for '{contract}'")
await client.send_message(monitor, "/safe " + contract + "\n")
except ChatAdminRequiredError:
logger.error(
f"Not enough priviledge to write to telegram channel {monitor}"
)
else:
logger.debug(f"Could not extract contract from {line}")
else:
logger.debug("No valid trigger message")
else:
logger.debug("No valid trigger message")
loop = asyncio.get_event_loop()
loop.run_until_complete(setup())
client.run_until_disconnected()