本文整理汇总了Python中telegram.MessageEntity.TEXT_MENTION属性的典型用法代码示例。如果您正苦于以下问题:Python MessageEntity.TEXT_MENTION属性的具体用法?Python MessageEntity.TEXT_MENTION怎么用?Python MessageEntity.TEXT_MENTION使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类telegram.MessageEntity
的用法示例。
在下文中一共展示了MessageEntity.TEXT_MENTION属性的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_all_members
# 需要导入模块: from telegram import MessageEntity [as 别名]
# 或者: from telegram.MessageEntity import TEXT_MENTION [as 别名]
def set_all_members(bot, update, **kwargs):
"""Set members to be tagged when @all keyword is used."""
msg = kwargs.get('args')
if not msg:
logger.info(
"No users passed to set_all_members function. kwargs: %s", kwargs)
return
user_entities = update.message.parse_entities(
[MessageEntity.MENTION, MessageEntity.TEXT_MENTION]
)
updated = update_all_users(user_entities)
if updated:
bot.send_message(
chat_id=update.message.chat_id, text='Users added to the @all tag'
)
else:
pass
bot.send_message(
chat_id=update.message.chat_id,
text='Algo pasó. Hablale a @BoedoCrow y pedile que vea los logs.',
)
示例2: reply_afk
# 需要导入模块: from telegram import MessageEntity [as 别名]
# 或者: from telegram.MessageEntity import TEXT_MENTION [as 别名]
def reply_afk(bot: Bot, update: Update):
message = update.effective_message # type: Optional[Message]
entities = message.parse_entities([MessageEntity.TEXT_MENTION, MessageEntity.MENTION])
if message.entities and entities:
for ent in entities:
if ent.type == MessageEntity.TEXT_MENTION:
user_id = ent.user.id
fst_name = ent.user.first_name
elif ent.type == MessageEntity.MENTION:
user_id = get_user_id(message.text[ent.offset:ent.offset + ent.length])
if not user_id:
# Should never happen, since for a user to become AFK they must have spoken. Maybe changed username?
return
chat = bot.get_chat(user_id)
fst_name = chat.first_name
else:
return
if sql.is_afk(user_id):
valid, reason = sql.check_afk_status(user_id)
if valid:
if not reason:
res = "{} isn't here bud.".format(fst_name)
else:
res = "{} is AFK! says its because of:\n{}".format(fst_name, reason)
message.reply_text(res)
示例3: reply_afk
# 需要导入模块: from telegram import MessageEntity [as 别名]
# 或者: from telegram.MessageEntity import TEXT_MENTION [as 别名]
def reply_afk(update, context):
message = update.effective_message # type: Optional[Message]
entities = message.parse_entities([MessageEntity.TEXT_MENTION, MessageEntity.MENTION])
if message.entities and entities:
for ent in entities:
if ent.type == MessageEntity.TEXT_MENTION:
user_id = ent.user.id
fst_name = ent.user.first_name
elif ent.type == MessageEntity.MENTION:
user_id = get_user_id(message.text[ent.offset:ent.offset + ent.length])
if not user_id:
# Should never happen, since for a user to become AFK they must have spoken. Maybe changed username?
return
try:
chat = context.bot.get_chat(user_id)
except BadRequest:
print("Error: Could not fetch userid {} for AFK module".format(user_id))
return
fst_name = chat.first_name
else:
return
if sql.is_afk(user_id):
valid, reason = sql.check_afk_status(user_id)
if valid:
if not reason:
res = tl(update.effective_message, "{} sedang AFK!").format(fst_name)
else:
res = tl(update.effective_message, "{} sedang AFK!\nKarena : {}").format(fst_name, reason)
send_message(update.effective_message, res)
示例4: reply_afk
# 需要导入模块: from telegram import MessageEntity [as 别名]
# 或者: from telegram.MessageEntity import TEXT_MENTION [as 别名]
def reply_afk(bot: Bot, update: Update):
message = update.effective_message # type: Optional[Message]
if message.entities and message.parse_entities([MessageEntity.TEXT_MENTION, MessageEntity.MENTION]):
entities = message.parse_entities([MessageEntity.TEXT_MENTION, MessageEntity.MENTION])
for ent in entities:
if ent.type == MessageEntity.TEXT_MENTION:
user_id = ent.user.id
fst_name = ent.user.first_name
elif ent.type == MessageEntity.MENTION:
user_id = get_user_id(message.text[ent.offset:ent.offset + ent.length])
if not user_id:
# Should never happen, since for a user to become AFK they must have spoken. Maybe changed username?
return
chat = bot.get_chat(user_id)
fst_name = chat.first_name
else:
return
if sql.is_afk(user_id):
user = sql.check_afk_status(user_id)
if not user.reason:
res = "{} is away from the keyboard ! reason :\n{} ".format(fst_name)
else:
res = "{} is away from the keyboard ! reason :\n{}. ".format(fst_name, user.reason)
message.reply_text(res)
示例5: reply_afk
# 需要导入模块: from telegram import MessageEntity [as 别名]
# 或者: from telegram.MessageEntity import TEXT_MENTION [as 别名]
def reply_afk(bot: Bot, update: Update):
message = update.effective_message # type: Optional[Message]
entities = message.parse_entities([MessageEntity.TEXT_MENTION, MessageEntity.MENTION])
if message.entities and entities:
for ent in entities:
if ent.type == MessageEntity.TEXT_MENTION:
user_id = ent.user.id
fst_name = ent.user.first_name
elif ent.type == MessageEntity.MENTION:
user_id = get_user_id(message.text[ent.offset:ent.offset + ent.length])
if not user_id:
# Should never happen, since for a user to become AFK they must have spoken. Maybe changed username?
return
chat = bot.get_chat(user_id)
fst_name = chat.first_name
else:
return
if sql.is_afk(user_id):
valid, reason = sql.check_afk_status(user_id)
if valid:
if not reason:
res = "{} is AFK!".format(fst_name)
else:
res = "{} is AFK! says its because of:\n{}".format(fst_name, reason)
message.reply_text(res)
示例6: info
# 需要导入模块: from telegram import MessageEntity [as 别名]
# 或者: from telegram.MessageEntity import TEXT_MENTION [as 别名]
def info(bot: Bot, update: Update, args: List[str]):
msg = update.effective_message # type: Optional[Message]
user_id = extract_user(update.effective_message, args)
if user_id:
user = bot.get_chat(user_id)
elif not msg.reply_to_message and not args:
user = msg.from_user
elif not msg.reply_to_message and (not args or (
len(args) >= 1 and not args[0].startswith("@") and not args[0].isdigit() and not msg.parse_entities(
[MessageEntity.TEXT_MENTION]))):
msg.reply_text("Yeah nah, this mans doesn't exist.")
return
else:
return
text = "<b>User info</b>:" \
"\nID: <code>{}</code>" \
"\nFirst Name: {}".format(user.id, html.escape(user.first_name))
if user.last_name:
text += "\nLast Name: {}".format(html.escape(user.last_name))
if user.username:
text += "\nUsername: @{}".format(html.escape(user.username))
text += "\nPermanent user link: {}".format(mention_html(user.id, "link"))
if user.id == OWNER_ID:
text += "\n\nDis nibba stronk af!"
else:
if user.id in SUDO_USERS:
text += "\nThis person is one of my sudo users! " \
"Nearly as powerful as my owner - so watch it."
else:
if user.id in SUPPORT_USERS:
text += "\nThis person is one of my support users! " \
"Not quite a sudo user, but can still gban you off the map."
if user.id in WHITELIST_USERS:
text += "\nThis person is Anirudh! " \
"I feel bad for them, so I'm not going to ban/kick them."
for mod in USER_INFO:
mod_info = mod.__user_info__(user.id).strip()
if mod_info:
text += "\n\n" + mod_info
update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
示例7: extract_user_and_text
# 需要导入模块: from telegram import MessageEntity [as 别名]
# 或者: from telegram.MessageEntity import TEXT_MENTION [as 别名]
def extract_user_and_text(message: Message, args: List[str]) -> (Optional[int], Optional[str]):
prev_message = message.reply_to_message
split_text = message.text.split(None, 1)
if len(split_text) < 2:
return id_from_reply(message) # only option possible
text_to_parse = split_text[1]
text = ""
entities = list(message.parse_entities([MessageEntity.TEXT_MENTION]))
if len(entities) > 0:
ent = entities[0]
else:
ent = None
# if entity offset matches (command end/text start) then all good
if entities and ent and ent.offset == len(message.text) - len(text_to_parse):
ent = entities[0]
user_id = ent.user.id
text = message.text[ent.offset + ent.length:]
elif len(args) >= 1 and args[0][0] == '@':
user = args[0]
user_id = get_user_id(user)
if not user_id:
return "error", "Saya tidak memiliki pengguna di db saya. Anda akan dapat berinteraksi dengan mereka jika Anda membalas pesan orang itu, atau meneruskan salah satu dari pesan pengguna itu."
else:
user_id = user_id
res = message.text.split(None, 2)
if len(res) >= 3:
text = res[2]
elif len(args) >= 1 and args[0].isdigit():
user_id = int(args[0])
res = message.text.split(None, 2)
if len(res) >= 3:
text = res[2]
elif prev_message:
user_id, text = id_from_reply(message)
else:
return None, None
try:
message.bot.get_chat(user_id)
except BadRequest as excp:
if excp.message in ("User_id_invalid", "Chat not found"):
return "error", "Saya sepertinya tidak pernah berinteraksi dengan pengguna ini sebelumnya - silakan meneruskan pesan dari mereka untuk memberi saya kontrol! (Seperti boneka voodoo, saya butuh sepotong untuk bisa untuk menjalankan perintah tertentu...)"
else:
LOGGER.exception("Exception %s on user %s", excp.message, user_id)
return None, None
return user_id, text
示例8: info
# 需要导入模块: from telegram import MessageEntity [as 别名]
# 或者: from telegram.MessageEntity import TEXT_MENTION [as 别名]
def info(bot: Bot, update: Update, args: List[str]):
msg = update.effective_message # type: Optional[Message]
user_id = extract_user(update.effective_message, args)
if user_id:
user = bot.get_chat(user_id)
elif not msg.reply_to_message and not args:
user = msg.from_user
elif not msg.reply_to_message and (not args or (
len(args) >= 1 and not args[0].startswith("@") and not args[0].isdigit() and not msg.parse_entities(
[MessageEntity.TEXT_MENTION]))):
msg.reply_text("I can't extract a user from this.")
return
else:
return
text = "<b>User info</b>:" \
"\nID: <code>{}</code>" \
"\nFirst Name: {}".format(user.id, html.escape(user.first_name))
if user.last_name:
text += "\nLast Name: {}".format(html.escape(user.last_name))
if user.username:
text += "\nUsername: @{}".format(html.escape(user.username))
text += "\nPermanent user link: {}".format(mention_html(user.id, "link"))
if user.id == OWNER_ID:
text += "\n\nThis person is my owner - I would never do anything against them!"
else:
if user.id in SUDO_USERS:
text += "\nThis person is one of my sudo users! " \
"Nearly as powerful as my owner - so watch it."
else:
if user.id in SUPPORT_USERS:
text += "\nThis person is one of my support users! " \
"Not quite a sudo user, but can still gban you off the map."
if user.id in WHITELIST_USERS:
text += "\nThis person has been whitelisted! " \
"That means I'm not allowed to ban/kick them."
for mod in USER_INFO:
mod_info = mod.__user_info__(user.id).strip()
if mod_info:
text += "\n\n" + mod_info
update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)