-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_telegram_message.py
More file actions
95 lines (75 loc) · 3.71 KB
/
send_telegram_message.py
File metadata and controls
95 lines (75 loc) · 3.71 KB
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
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes
import os
from dotenv import load_dotenv
import subprocess
import asyncio
load_dotenv()
# Get credentials and paths from .env file
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
CHAT_ID = os.getenv('CHAT_ID')
SCREENSHOT_PATH = os.getenv('SCREENSHOT_PATH')
SCRIPT_PATH = os.getenv('SCRIPT_PATH')
async def send_message(application, chat_id: str, message: str):
await application.bot.send_message(chat_id=chat_id, text=message)
print(f"Sent message to {chat_id}: {message}")
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
print("Received /start command")
await send_message(context.application, update.effective_chat.id, 'Do you want to register the point? Reply "Yes" to confirm or "No" to take a screenshot of the current page.')
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Convert the text to lowercase to handle all variations
text = update.message.text.lower()
print(f"Received message: {text}")
if text in ["yes", "y"]:
print("Processing 'Yes' response")
# Call the point registration script
result = subprocess.run(
['python3', SCRIPT_PATH], capture_output=True, text=True)
if result.returncode == 0:
await update.message.reply_text('Point registered successfully!')
else:
await update.message.reply_text(f'Error registering point: {result.stderr}')
# Send a screenshot after registering the point
screenshot_file = os.path.join(
SCREENSHOT_PATH, 'screenshot_after_register.png')
print(f"Looking for screenshot: {screenshot_file}")
if os.path.exists(screenshot_file):
with open(screenshot_file, 'rb') as photo:
await context.bot.send_photo(chat_id=update.effective_chat.id, photo=photo)
print(
f"Sent screenshot after register to {update.effective_chat.id}")
else:
await update.message.reply_text('Screenshot not found.')
elif text in ["no", "n"]:
print("Processing 'No' response")
# Just take a screenshot of the current page after login
screenshot_file = os.path.join(
SCREENSHOT_PATH, 'screenshot_after_login.png')
print(f"Looking for screenshot: {screenshot_file}")
# Take a screenshot (assuming your script can handle taking a screenshot without performing other actions)
result = subprocess.run(
['python3', SCRIPT_PATH, '--screenshot'], capture_output=True, text=True)
if result.returncode == 0:
await update.message.reply_text('Current page screenshot taken.')
else:
await update.message.reply_text(f'Error taking screenshot: {result.stderr}')
if os.path.exists(screenshot_file):
with open(screenshot_file, 'rb') as photo:
await context.bot.send_photo(chat_id=update.effective_chat.id, photo=photo)
print(
f"Sent current page screenshot to {update.effective_chat.id}")
else:
await update.message.reply_text('Screenshot not found.')
async def main():
application = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(MessageHandler(
filters.TEXT & ~filters.COMMAND, handle_message))
await application.initialize()
await application.start()
print("Bot is running...")
# Start polling for updates
await application.updater.start_polling()
await application.idle()
if __name__ == '__main__':
asyncio.run(main())