-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
59 lines (47 loc) · 1.9 KB
/
bot.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
import getopt
import sys
import re
import requests
from telethon.errors import SessionPasswordNeededError
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
PeerChannel
)
def startListener(opts):
api_id = int(opts[0].__getitem__(1))
api_hash = opts[1].__getitem__(1)
source_username = opts[2].__getitem__(1)
target_username = opts[3].__getitem__(1)
shortURLRegex = r"https?:\/\/(([^\s]*)\.)?amzn\.to\/([0-9A-Za-z]+)"
user_input_channel = 'https://t.me/' + source_username
client = TelegramClient('session_name', api_id, api_hash)
client.start()
@client.on(events.NewMessage(chats=user_input_channel))
async def newMessageListener(event):
messageFromEvent = event.message.message
print("Received event with message:\n" + messageFromEvent)
filteredMessage = re.findall(
shortURLRegex, messageFromEvent, flags=re.IGNORECASE)
if len(filteredMessage) != 0:
response = requests.get('http://telegram-bot-amzn.herokuapp.com')
print('Made a GET requrest to bot, got {status}'.format(
status=str(response.status_code)))
await client.send_message(target_username, messageFromEvent)
with client:
client.run_until_disconnected()
argv = sys.argv[1:]
usage = "usage: bot.py --api_id=<api_id> --api_hash=<api_hash> --source_username=<source_username> --target_username=<target_username>"
try:
opts, args = getopt.getopt(
argv, 'a:b:c:d:', ['api_id=', 'api_hash=', 'source_username=', 'target_username='])
# Check if the options' length is 4 (can be enhanced)
if len(opts) == 0 or len(opts) > 4:
print(usage)
else:
# Start the listener
print("Starting the listener")
startListener(opts)
except getopt.GetoptError:
print(usage)
sys.exit(1)