Last active
May 8, 2024 14:01
-
-
Save kimsama/d80ec2e1ac0f02a9597f80bf46d38826 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Modified echo example of https://github.com/python-telegram-bot | |
# | |
# - print message in the terminal | |
# - send back echo message in the telegram channel | |
# | |
import logging | |
from telegram import ForceReply, Update | |
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters | |
TOKEN = '{YOUR_TOKEN_HERE}' | |
# 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"Hi {user.mention_html()}!", | |
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("Help!") | |
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: | |
"""Handle both direct messages and channel posts and print the text in the terminal.""" | |
# If the update contains a channel post, reply to the channel post text | |
if update.channel_post and update.channel_post.text: | |
text = update.channel_post.text | |
chat_id = update.channel_post.chat.id | |
# Print to the terminal | |
print(f"Channel post received from {chat_id}: {text}") | |
# Reply by sending a message to the channel | |
await context.bot.send_message(chat_id=chat_id, text=text) | |
# Otherwise, check for a standard message | |
elif update.message and update.message.text: | |
text = update.message.text | |
chat_id = update.message.chat.id | |
# Print to the terminal | |
print(f"Message received from {chat_id}: {text}") | |
# Reply directly to the message | |
await update.message.reply_text(text) | |
else: | |
# Log details for debugging when neither type is found | |
logger.warning(f"Received update without recognizable message content: {update.to_dict()}") | |
def main() -> None: | |
"""Start the bot.""" | |
# Create the Application and pass it your bot's token. | |
application = Application.builder().token(TOKEN).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) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment