0%

自动获取Pocket发送到Telegram

分享到的每日资讯telegram频道:https://t.me/hellolinuxRss

准备工作

  • 创建一个机器人

    • 在 Teletram 中搜索用户 @BotFather
  • 发送命令 /newbot 并为你的机器人指定 nameusername

    • 拿到 token 并记录在一个安全的地方,后边会用到。
  • Use this token to access the HTTP API:

  • 创建一个频道并把这个bot添加进去作为管理员

发送消息

curl测试

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

requests测试

1
2
3
4
5
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)

对话机器人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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