【问题标题】:How to tag and reply to welcome a member in a welcome bot Telegram?如何在欢迎机器人电报中标记和回复欢迎成员?
【发布时间】:2022-01-01 01:31:47
【问题描述】:

我有这个来自 github 的电报欢迎机器人。我希望每次有新用户加入群组时,除了向他发送提到的消息并回复“id 已加入群组”,就像经典群组帮助一样。

我在下面附上要处理的代码:

@run_async
def send_async(context, *args, **kwargs):
    context.bot.send_message(*args, **kwargs)


def check(update, context, override_lock=None):
    """
    Perform some checks on the update. If checks were successful, returns True,
    else sends an error message to the chat and returns False.
    """

    chat_id = update.message.chat_id
    chat_str = str(chat_id)

    if chat_id > 0:
        send_async(
            context, chat_id=chat_id, text="Please add me to a group first!",
        )
        return False

    locked = override_lock if override_lock is not None else db.get(chat_str + "_lck")

    if locked and db.get(chat_str + "_adm") != update.message.from_user.id:
        if not db.get(chat_str + "_quiet"):
            send_async(
                context,
                chat_id=chat_id,
                text="Sorry, only the person who invited me can do that.",
            )
        return False

    return True


# Welcome a user to the chat
def welcome(update, context, new_member):
    """ Welcomes a user to the chat """

    message = update.message
    chat_id = message.chat.id
    logger.info(
        "%s joined to chat %d (%s)",
        escape(new_member.first_name),
        chat_id,
        escape(message.chat.title),
    )
    # Pull the custom message for this chat from the database
    text = db.get(str(chat_id))

    # Use default message if there's no custom one set
    if text is None:
        text = "Hello $username! Welcome to $title ????"

    # Replace placeholders and send message
    text = text.replace("$username", new_member.first_name)
    text = text.replace("$title", message.chat.title)
    
    send_async(context, chat_id=chat_id, text=text, parse_mode=ParseMode.HTML)

这是我想要达到的结果。

有人知道如何帮助我完成这个简单的任务吗?谢谢!

【问题讨论】:

  • 你目前的代码有什么问题?为什么不符合您的要求? How to Ask
  • 我目前拥有的代码没有提及和回复加入该组的用户。所以它不会做我要求的任何事情。

标签: python bots telegram


【解决方案1】:

这是复杂的代码...保持简单。

您在群组"new chat members" 中有一个活动。所以请处理此事件并使用first_name 发送回复并链接到该帐户。

Pyrogram 示例:

from pyrogram import Client
from pyrogram import filters


ACCOUNT = '77777777777'
config = 'config.ini'

__client = Client(session_name=ACCOUNT, config_file=config)

def user_link(user):
  USER_LINK = '<a href="tg://resolve?domain={username}">{first_name} : @{username}</a>'
  USERID_LINK = '<a href="tg://user?id={id}">{first_name} : {id}</a>'
  return USER_LINK.format(**user.__dict__) if user.username else USERID_LINK.format(**user.__dict__)

@__client.on_message(filters.new_chat_members)
def welcome(client, message):
  users = ", ".join([user_link(message.from_user.__dict__) for m in message.new_chat_members])
  message.reply(f"Hello, {users}")

【讨论】:

    猜你喜欢
    • 2018-02-07
    • 2021-06-01
    • 1970-01-01
    • 2015-10-02
    • 2020-05-18
    • 1970-01-01
    • 2021-02-27
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多