hellolinux2021
hellolinux2021

欢迎拍手与交流,个人Channel:https://t.me/hellolinuxLab Sentinel DVPN中文社区管理,技术及使用支持 https://t.me/Sentinel_China 个人主页:https://hellolinux.uk/about

Automatically get Pocket to send to Telegram

Continue the previous article

Daily news telegram channel shared to: https://t.me/hellolinuxRss

The typesetting is normal to see the original text: Automatically obtain Pocket and send it to Telegram

Preparation

  • Create a bot
  • Search Teletram for user @BotFather
  • Send the command /newbot and give your bot a name and username
  • Get the token and record it in a safe place, it will be used later.
  • Use this token to access the HTTP API:

For a description of the Bot API, see this page: https://core.telegram.org/bots/api

  • Create a channel and add the bot as admin
  • And send any message to @bot in the channel, and then enter in the browser: https://api.telegram.org/bot {token}/getUpdates to get the corresponding chat_id, which will be used later, if there are multiple, you can During the interview, determine which one you need.

Send a message

curl test

 curl -s -X POST https://api.telegram.org/bot{token}/sendMessage -d chat_id={chatId} -d text="Hello World"

requests test

 import requests
token = "1876xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2NhVRI" # Replace with your own token
chat_id = 130000006 # Replace with your own chat_id
r = requests.post(f'https://api.telegram.org/bot{token}/sendMessage', json={"chat_id": chat_id, "text": "[Bot] Test Message"})
print(r.json())

python-telegram-bot

https://docs.python-telegram-bot.org/en/stable

Install the python library: pip install python-telegram-bot --upgrade

 import telegram
​
chat_id = ''
token = ''
bot = telegram.Bot(token)
​
bot.send_message(chat_id=chat_id,text='hello world')
​
bot.send_message(
chat_id=chat_id,
text='<a href="https://hellolinux.xyz/">hellolinux's personal blog</a>',
parse_mode=telegram.ParseMode.HTML
)
bot.send_message(chat_id=chat_id,text='[hellolinux's personal blog](https://hellolinux.xyz/)',parse_mode=telegram.ParseMode.MARKDOWN)

conversation bot

 from telegram.ext.updater import Updater
from telegram.update import Update
from telegram.ext.callbackcontext import CallbackContext
from telegram.ext.commandhandler import CommandHandler
from telegram.ext.messagehandler import MessageHandler
from telegram.ext.filters import Filters
​
updater = Updater("your_own_API_Token got from BotFather",
use_context=True)
​
​
def start(update: Update, context: CallbackContext):
update.message.reply_text(
"Hello sir, Welcome to the Bot. Please write\
/help to see the commands available.")
​
def help(update: Update, context: CallbackContext):
update.message.reply_text("""Available Commands :-
 /youtube - To get the youtube URL
 /linkedin - To get the LinkedIn profile URL
 /gmail - To get gmail URL
 /geeks - To get the GeeksforGeeks URL""")
​
​
def gmail_url(update: Update, context: CallbackContext):
update.message.reply_text(
"Your gmail link here (I am not\
giving mine one for security reasons)")
​
​
def youtube_url(update: Update, context: CallbackContext):
update.message.reply_text("Youtube Link =>\
  https://www.youtube.com/")
​
​
def linkedIn_url(update: Update, context: CallbackContext):
update.message.reply_text(
"LinkedIn URL => \
 https://www.linkedin.com/in/dwaipayan-bandyopadhyay-007a/")
​
​
def geeks_url(update: Update, context: CallbackContext):
update.message.reply_text(
"GeeksforGeeks URL => https://www.geeksforgeeks.org/")
​
​
def unknown(update: Update, context: CallbackContext):
update.message.reply_text(
"Sorry '%s' is not a valid command" % update.message.text)
​
​
def unknown_text(update: Update, context: CallbackContext):
update.message.reply_text(
"Sorry I can't recognize you , you said '%s'" % update.message.text)
​
​
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('youtube', youtube_url))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_handler(CommandHandler('linkedin', linkedIn_url))
updater.dispatcher.add_handler(CommandHandler('gmail', gmail_url))
updater.dispatcher.add_handler(CommandHandler('geeks', geeks_url))
updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown))
updater.dispatcher.add_handler(MessageHandler(
Filters.command, unknown)) # Filters out unknown commands
​
# Filters out unknown messages.
updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown_text))
​
updater.start_polling()
​

pyTelegramBotAPI library

https://github.com/eternnoir/pyTelegramBotAPI

refer to

https://core.telegram.org/bots/api#sendmessage

https://docs.python-telegram-bot.org/en/stable/telegram.parsemode.html

CC BY-NC-ND 2.0

Like my work?
Don't forget to support or like, so I know you are with me..

Loading...
Loading...

Comment