自動獲取Pocket發送到Telegram

hellolinux2021
·
·
IPFS
·
接之前的文章

分享到的每日資訊telegram頻道: https://t.me/hellolinuxRss

排版正常看原文:自動獲取Pocket發送到Telegram

準備工作

  • 創建一個機器人
  • 在Teletram 中搜索用戶@BotFather
  • 發送命令/newbot並為你的機器人指定nameusername
  • 拿到token 並記錄在一個安全的地方,後邊會用到。
  • Use this token to access the HTTP API:

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

  • 創建一個頻道並把這個bot添加進去作為管理員
  • 並在頻道中@bot 發送任何消息,再在瀏覽器中輸入: https://api.telegram.org/bot {token}/getUpdates,獲取對應chat_id,後面會用到,如果有多個,可以後面試下,確認哪個是自己需要的。

發送消息

curl測試

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

requests測試

import requests
token = "1876xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2NhVRI" # 替換成你自己的token
chat_id = 130000006 # 替換成你自己的chat_id
r = requests.post(f'https://api.telegram.org/bot{token}/sendMessage', json={"chat_id": chat_id, "text": "【機器人】測試消息"})
print(r.json())

python-telegram-bot

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

安裝python庫: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的個人博客</a>',
  parse_mode=telegram.ParseMode.HTML
)
bot.send_message(chat_id=chat_id,text='[hellolinux的個人博客](https://hellolinux.xyz/)',parse_mode=telegram.ParseMode.MARKDOWN)

對話機器人

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庫

https://github.com/eternnoir/pyTelegramBotAPI

參考

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

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

CC BY-NC-ND 2.0 授權

喜歡我的作品嗎?別忘了給予支持與讚賞,讓我知道在創作的路上有你陪伴,一起延續這份熱忱!

hellolinux2021欢迎拍手与交流,个人Channel:https://t.me/hellolinuxLab Sentinel DVPN中文社区管理,技术及使用支持 https://t.me/Sentinel_China 个人主页:https://hellolinux.uk/about
  • 來自作者
  • 相關推薦

20220820近况更新

费曼学习法

obsidian的annotator插件