Last active
July 31, 2022 07:12
-
-
Save Gowee/e9fff855711e401e11ab3a57027561c1 to your computer and use it in GitHub Desktop.
Backup 2017-01.
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Backup 2017-01-07. | |
from uuid import uuid4 | |
from telegram import InlineQueryResultArticle, ParseMode, \ | |
InputTextMessageContent | |
from telegram.ext import Updater, InlineQueryHandler, CommandHandler | |
import logging | |
# Enable logging | |
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
# Define a few command handlers. These usually take the two arguments bot and | |
# update. Error handlers also receive the raised TelegramError object in error. | |
def start(bot, update): | |
update.message.reply_text('唔。') | |
def help(bot, update): | |
update.message.reply_text('蛤?') | |
def inlinequery(bot, update): | |
query = update.inline_query.query | |
results = list() | |
results.append(InlineQueryResultArticle(id=uuid4(), | |
title="Markdown", | |
input_message_content=InputTextMessageContent( | |
query, parse_mode=ParseMode.MARKDOWN))) | |
results.append(InlineQueryResultArticle(id=uuid4(), | |
title="HTML", | |
input_message_content=InputTextMessageContent( | |
query, parse_mode=ParseMode.HTML))) | |
update.inline_query.answer(results) | |
def error(bot, update, error): | |
logger.warn('Update "%s" caused error "%s"' % (update, error)) | |
def main(): | |
# Create the Updater and pass it your bot's token. | |
updater = Updater("32...:...c") | |
# Get the dispatcher to register handlers | |
dp = updater.dispatcher | |
# on different commands - answer in Telegram | |
dp.add_handler(CommandHandler("start", start)) | |
dp.add_handler(CommandHandler("help", help)) | |
# on noncommand i.e message - echo the message on Telegram | |
dp.add_handler(InlineQueryHandler(inlinequery)) | |
# log all errors | |
dp.add_error_handler(error) | |
# Start the Bot | |
updater.start_polling() | |
# Block until the user presses Ctrl-C or the process receives SIGINT, | |
# SIGTERM or SIGABRT. This should be used most of the time, since | |
# start_polling() is non-blocking and will stop the bot gracefully. | |
updater.idle() | |
if __name__ == '__main__': | |
main()⏎ |
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Backup 2017-01-23. | |
from uuid import uuid4 | |
import re | |
from telegram import InlineQueryResultArticle, ParseMode, \ | |
InputTextMessageContent | |
from telegram.ext import Updater, InlineQueryHandler, CommandHandler | |
import logging | |
# Enable logging | |
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
level=logging.WARNING) | |
logger = logging.getLogger(__name__) | |
strikethrough_pattern_markdown = re.compile(r"\~\~(.+)\~\~") | |
strikethrough_pattern_html = re.compile(r"\<del\>(.+)\<\/del\>") | |
def strikethrough(text): | |
if len(text) == 0: | |
return "" | |
return "\u0336" + "\u0336".join(list(text)) + "\u0336" | |
def format_strikethrough_markdown(text): | |
return strikethrough_pattern_markdown.sub(lambda m: strikethrough(m.group(1)) | |
, text) | |
def format_strikethrough_html(text): | |
return strikethrough_pattern_html.sub(lambda m: strikethrough(m.group(1)) | |
, text) | |
# Define a few command handlers. These usually take the two arguments bot and | |
# update. Error handlers also receive the raised TelegramError object in error. | |
def start(bot, update): | |
update.message.reply_text('A _inline bot_ to help you send `Markdown/HTML` \ | |
formatted message, to learn about supported syntax, *send* /help.', | |
parse_mode=ParseMode.MARKDOWN) | |
def help(bot, update): | |
update.message.reply_text('*Docs:*\n[Markdown syntax guides]\ | |
(https://core.telegram.org/bots/api#markdown-style)\n\ | |
[HTML syntax guides]\ | |
(https://core.telegram.org/bots/api#html-style)\n\ | |
And this bot also support `~~Strike~~(Markdown) or <del>through</del>(HTML)`, such as ' + | |
strikethrough("this") + ".", parse_mode=ParseMode.MARKDOWN) | |
def inlinequery(bot, update): | |
query = update.inline_query.query | |
results = list() | |
results.append(InlineQueryResultArticle(id=uuid4(), | |
title="Markdown", | |
input_message_content=InputTextMessageContent( | |
format_strikethrough_markdown(query), | |
parse_mode=ParseMode.MARKDOWN, | |
disable_web_page_preview=True))) | |
#print(results[0]) | |
results.append(InlineQueryResultArticle(id=uuid4(), | |
title="HTML", | |
input_message_content=InputTextMessageContent( | |
format_strikethrough_html(query), | |
parse_mode=ParseMode.HTML))) | |
update.inline_query.answer(results) | |
def error(bot, update, error): | |
logger.warn('Update "%s" caused error "%s"' % (update, error)) | |
def main(): | |
# Create the Updater and pass it your bot's token. | |
updater = Updater("31...:...-D...") | |
# Get the dispatcher to register handlers | |
dp = updater.dispatcher | |
# on different commands - answer in Telegram | |
dp.add_handler(CommandHandler("start", start)) | |
dp.add_handler(CommandHandler("help", help)) | |
# on noncommand i.e message - echo the message on Telegram | |
dp.add_handler(InlineQueryHandler(inlinequery)) | |
# log all errors | |
dp.add_error_handler(error) | |
# Start the Bot | |
updater.start_polling() | |
# Block until the user presses Ctrl-C or the process receives SIGINT, | |
# SIGTERM or SIGABRT. This should be used most of the time, since | |
# start_polling() is non-blocking and will stop the bot gracefully. | |
updater.idle() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment