本文整理汇总了Python中telegram.error.BadRequest方法的典型用法代码示例。如果您正苦于以下问题:Python error.BadRequest方法的具体用法?Python error.BadRequest怎么用?Python error.BadRequest使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类telegram.error
的用法示例。
在下文中一共展示了error.BadRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward_faq
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def forward_faq(update: Update, context: CallbackContext):
if update.message.chat.username not in [ONTOPIC_USERNAME, OFFTOPIC_USERNAME]:
return
admins = context.bot.get_chat_administrators(ONTOPIC_USERNAME)
if update.effective_user.id not in [x.user.id for x in admins]:
return
if not update.message:
return
reply_to = update.message.reply_to_message
if not reply_to:
return
try:
update.message.delete()
except BadRequest:
pass
# Forward message to FAQ channel
reply_to.forward(const.FAQ_CHANNEL_ID, disable_notification=True)
示例2: del_blacklist
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def del_blacklist(bot: Bot, update: Update):
chat = update.effective_chat # type: Optional[Chat]
message = update.effective_message # type: Optional[Message]
to_match = extract_text(message)
if not to_match:
return
chat_filters = sql.get_chat_blacklist(chat.id)
for trigger in chat_filters:
pattern = r"( |^|[^\w])" + re.escape(trigger) + r"( |$|[^\w])"
if re.search(pattern, to_match, flags=re.IGNORECASE):
try:
message.delete()
except BadRequest as excp:
if excp.message == "Message to delete not found":
pass
else:
LOGGER.exception("Error while deleting blacklist message.")
break
示例3: getlink
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def getlink(bot: Bot, update: Update, args: List[int]):
message = update.effective_message
if args:
pattern = re.compile(r'-\d+')
else:
message.reply_text("You don't seem to be referring to any chats.")
links = "Invite link(s):\n"
for chat_id in pattern.findall(message.text):
try:
chat = bot.getChat(chat_id)
bot_member = chat.get_member(bot.id)
if bot_member.can_invite_users:
invitelink = bot.exportChatInviteLink(chat_id)
links += str(chat_id) + ":\n" + invitelink + "\n"
else:
links += str(chat_id) + ":\nI don't have access to the invite link." + "\n"
except BadRequest as excp:
links += str(chat_id) + ":\n" + excp.message + "\n"
except TelegramError as excp:
links += str(chat_id) + ":\n" + excp.message + "\n"
message.reply_text(links)
示例4: pin
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def pin(bot: Bot, update: Update, args: List[str]) -> str:
user = update.effective_user # type: Optional[User]
chat = update.effective_chat # type: Optional[Chat]
is_group = chat.type != "private" and chat.type != "channel"
prev_message = update.effective_message.reply_to_message
is_silent = True
if len(args) >= 1:
is_silent = not (args[0].lower() == 'notify' or args[0].lower() == 'loud' or args[0].lower() == 'violent')
if prev_message and is_group:
try:
bot.pinChatMessage(chat.id, prev_message.message_id, disable_notification=is_silent)
except BadRequest as excp:
if excp.message == "Chat_not_modified":
pass
else:
raise
return "<b>{}:</b>" \
"\n#PINNED" \
"\n<b>Admin:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name))
return ""
示例5: unpin
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def unpin(bot: Bot, update: Update) -> str:
chat = update.effective_chat
user = update.effective_user # type: Optional[User]
try:
bot.unpinChatMessage(chat.id)
except BadRequest as excp:
if excp.message == "Chat_not_modified":
pass
else:
raise
return "<b>{}:</b>" \
"\n#UNPINNED" \
"\n<b>Admin:</b> {}".format(html.escape(chat.title),
mention_html(user.id, user.first_name))
示例6: send_or_edit
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def send_or_edit(self, chat_id, text, to_edit=None, **kwargs):
mid = to_edit
if isinstance(to_edit, Message):
mid = to_edit.message_id
try:
if to_edit:
return self.bot.edit_message_text(
text,
chat_id=chat_id,
message_id=mid,
**self._set_defaults(kwargs)
)
return self.send_message(chat_id, text=text, **self._set_defaults(kwargs))
except BadRequest as e:
if 'not modified' in e.message.lower():
pass
else:
return self.send_message(chat_id, text=text, **self._set_defaults(kwargs))
示例7: send_message
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def send_message(message, text, target_id=None, *args,**kwargs):
if not target_id:
try:
return message.reply_text(text, *args,**kwargs)
except error.BadRequest as err:
if str(err) == "Reply message not found":
try:
return message.reply_text(text, quote=False, *args, **kwargs)
except error.BadRequest as err:
LOGGER.exception("ERROR: {}".format(err))
elif str(err) == "Have no rights to send a message":
try:
dispatcher.bot.leaveChat(message.chat.id)
dispatcher.bot.sendMessage(DUMP_CHAT, "I am leave chat `{}`\nBecause of: `Muted`".format(message.chat.title))
except error.BadRequest as err:
if str(err) == "Chat not found":
pass
else:
LOGGER.exception("ERROR: {}".format(err))
else:
try:
dispatcher.bot.send_message(target_id, text, *args, **kwarg)
except error.BadRequest as err:
LOGGER.exception("ERROR: {}".format(err))
示例8: send_message_raw
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def send_message_raw(chat_id, text, *args, **kwargs):
try:
return dispatcher.bot.sendMessage(chat_id, text, *args,**kwargs)
except error.BadRequest as err:
if str(err) == "Reply message not found":
try:
if kwargs.get('reply_to_message_id'):
kwargs['reply_to_message_id'] = None
return dispatcher.bot.sendMessage(chat_id, text, *args,**kwargs)
except error.BadRequest as err:
LOGGER.exception("ERROR: {}".format(err))
'''elif str(err) == "Have no rights to send a message":
try:
dispatcher.bot.leaveChat(message.chat.id)
dispatcher.bot.sendMessage(DUMP_CHAT, "I am leave chat `{}`\nBecause of: `Muted`".format(message.chat.title))
except error.BadRequest as err:
if str(err) == "Chat not found":
pass'''
else:
LOGGER.exception("ERROR: {}".format(err))
示例9: echo
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def echo(update, context):
message = update.effective_message
chat_id = update.effective_chat.id
try:
message.delete()
except BadRequest:
pass
# Advanced
text, data_type, content, buttons = get_message_type(message)
tombol = build_keyboard_alternate(buttons)
if str(data_type) in ('Types.BUTTON_TEXT', 'Types.TEXT'):
try:
if message.reply_to_message:
context.bot.send_message(chat_id, text, parse_mode="markdown", reply_to_message_id=message.reply_to_message.message_id, disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup(tombol))
else:
context.bot.send_message(chat_id, text, quote=False, disable_web_page_preview=True, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(tombol))
except BadRequest:
context.bot.send_message(chat_id, tl(update.effective_message, "Teks markdown salah!\nJika anda tidak tahu apa itu markdown, silahkan ketik `/markdownhelp` pada PM."), parse_mode="markdown")
return
示例10: get_fed_log
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def get_fed_log(fed_id):
fed_setting = FEDERATION_BYFEDID.get(str(fed_id))
if fed_setting == None:
fed_setting = False
return fed_setting
if fed_setting.get('flog') == None:
return False
elif fed_setting.get('flog'):
try:
dispatcher.bot.get_chat(fed_setting.get('flog'))
except BadRequest:
set_fed_log(fed_id, None)
return False
except Unauthorized:
set_fed_log(fed_id, None)
return False
return fed_setting.get('flog')
else:
return False
示例11: distribute_tasks
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def distribute_tasks(bot, session):
"""Distribute tasks under idle maintenance chats."""
idle_maintenance_chats = (
session.query(Chat)
.filter(Chat.is_maintenance)
.filter(Chat.current_task_id.is_(None))
.all()
)
for chat in idle_maintenance_chats:
try:
tg_chat = call_tg_func(bot, "get_chat", args=[chat.id])
except BadRequest as e:
if e.message == "Chat not found": # noqa
session.delete(chat)
continue
raise e
try:
check_maintenance_chat(session, tg_chat, chat, job=True)
except (Unauthorized, ChatMigrated):
session.delete(chat)
session.commit()
示例12: cancel_tagging
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def cancel_tagging(self, bot):
"""Cancel the tagging process."""
if self.tag_mode == TagMode.STICKER_SET and self.current_sticker is not None:
keyboard = get_continue_tagging_keyboard(self.current_sticker.file_id)
try:
call_tg_func(
bot,
"edit_message_reply_markup",
[self.id, self.last_sticker_message_id],
{"reply_markup": keyboard},
)
except BadRequest as e:
# An update for a reply keyboard has failed (Probably due to button spam)
logger = logging.getLogger()
if "Message to edit not found" in str(
e
) or "Message is not modified" in str(e):
logger.info("Message to edit has been deleted.")
pass
else:
raise e
self.tag_mode = None
self.current_sticker = None
self.last_sticker_message_id = None
示例13: push_simple
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def push_simple(bot, chat_id, message):
try:
bot.sendMessage(chat_id=chat_id, text=message)
except BadRequest as e:
bot.sendMessage(chat_id=chat_id, text=replace_unsafe(message))
except RetryAfter as e:
sleep(240)
bot.sendMessage(chat_id=chat_id, text=message)
except TimedOut as e:
sleep(60)
bot.sendMessage(chat_id=chat_id, text=message)
except Unauthorized as e:
sleep(0.25)
except NetworkError as e:
sleep(30)
bot.sendMessage(chat_id=chat_id, text=message)
except Exception as e:
sleep(1)
bot.sendMessage(chat_id=chat_id, text=message)
示例14: run_job_queue
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def run_job_queue(bot):
try:
redix = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
while True:
# get job from redis
_, job = redix.brpop(TELEGRAM_WORKER_QUEUE)
job = json.loads(job)
message = "Hello this is your periodic star reminder and these are the lucky repos:\n"
for repo in job["repos"]:
message = "{}\n--\t[{}]({})".format(message, repo["name"], repo["url"])
message = "{}".format(message)
try:
bot.send_message(int(job['to']), message, parse_mode="Markdown", disable_web_page_preview=True)
except error.BadRequest as e:
logger.error("{}, UserID: {}".format(e, job["to"]))
except error.Unauthorized as e:
logger.error("{}, UserID: {}".format(e, job["to"]))
# report job done.
except KeyboardInterrupt:
raise KeyboardInterrupt
示例15: send_notifications_for_poll
# 需要导入模块: from telegram import error [as 别名]
# 或者: from telegram.error import BadRequest [as 别名]
def send_notifications_for_poll(context, session, poll, message_key):
"""Send the notifications for a single poll depending on the remaining time."""
locale = poll.locale
for notification in poll.notifications:
try:
# Get the chat and send the notification
tg_chat = context.bot.get_chat(notification.chat_id)
tg_chat.send_message(
i18n.t(message_key, locale=locale, name=poll.name),
parse_mode="markdown",
reply_to_message_id=notification.poll_message_id,
)
except BadRequest as e:
if e.message == "Chat not found":
session.delete(notification)
# Bot was removed from group
except Unauthorized:
session.delete(notification)