-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegramside.py
More file actions
53 lines (41 loc) · 2.14 KB
/
Copy pathtelegramside.py
File metadata and controls
53 lines (41 loc) · 2.14 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
import logging
from telegram import ForceReply, Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes, MessageHandler, filters
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
# Define a few command handlers. These usually take the two arguments update and
# context.
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /start is issued."""
user = update.effective_user
await update.message.reply_html(
rf"Started",
reply_markup=ForceReply(selective=True),
)
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /help is issued."""
await update.message.reply_text("not implemented")
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
message = update.message
sender = (message.from_user or message.sender_chat)
firstname = sender.first_name if sender.first_name != None else ""
lastname = sender.last_name if sender.last_name != None else ""
try:
await context.bot.send_message(chat_id=-1002374958503, text=f"`{firstname} {lastname} ({sender.username}):` {message.text}", parse_mode='MarkdownV2')
logging.info("Message sent successfully")
except Exception as e:
logging.error(f"Failed to send message: {str(e)}")
# Create the Application and pass it your bot's token.
application = ApplicationBuilder().token(token=open("telegramtoken.txt").read()).build()
# on different commands - answer in Telegram
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("help", help_command))
# on non command i.e message - echo the message on Telegram
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
# Run the bot until the user presses Ctrl-C
application.run_polling(allowed_updates=Update.ALL_TYPES)