发现没啥功能想继续实现的。也没有实际用途,弃坑了。

操作

在 Telegram 联系 @BotFather 来获取一个 bot。得到它的 API token 来操作。

这里使用了基于 Python 的 pyTelegramBotAPI

https://github.com/eternnoir/pyTelegramBotAPI

先写一个 /start 对应的命令,于是咱参考了「Writing Ur first bot」中的部分,于是内容是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import telebot

#from telebot import apihelper

#apihelper.proxy = {'https': 'socks5h://地址:端口' }

bot = telebot.TeleBot(‘TOKEN’, parse_mode=None) #

@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "欢迎使用 bot, 使用 /help 以查询功能。")

@bot.message_handler(func=lambda m: True)
def echo_all(message):
bot.reply_to(message, message.text)
#复读

bot.polling()
#反复检查消息,确保能够实时收到内容。

问题

首先遇到的问题是打开全局代理之后报错,关闭之后连不上。

这里使用了 Proxifier 来给 Python 走代理。

#127.0.0.1:[端口]

接下来遇到的问题就是类似于下文的状况:

requests.exceptions.SSLError: HTTPSConnectionPool(host=‘api.telegram.org’, port=4*3): Max retries exceeded with url: /bot[number]:[TOKEN]/getUpdates?offset=1&timeout=20 (Caused by SSLError(SSLEOFError(8, ’EOF occurred in violation of protocol (_ssl.c:1129)’)

https://stackoverflow.com/questions/68965686/requests-exceptions-connecttimeout-httpsconnectionpoolhost-api-telegram-org

上文的代码就是解决这个问题的方法。

1
2
3
from telebot import apihelper

apihelper.proxy = {'https': 'socks5h://地址:端口' }

这个时候出现了 「Missing dependencies for SOCKS support.」 的情况。Python 不支持走 socks5 代理,于是在命令行

1
pip install pysocks

https://stackoverflow.com/questions/38794015/pythons-requests-missing-dependencies-for-socks-support-when-using-socks5-fro

问题解决。

利用 bot.send_[format] 来回复

API 文档当中写的是

1
2
tb = telebot.Telebot<TOKEN>
tb.send_message(chat_id, text)

用法:

1
2
3
4
5
6
7
8
9
@bot.message_handler(commands=['photo'])
def send_reply_photo(message):
photo = open('1.png', 'rb')
bot.send_photo(message.chat.id, photo)

@bot.message_handler(commands=['getid'])
def send_ID(message):
bot.send_message(message.chat.id, message.chat.id)

chat_id = message.chat.id

message.chat.id 得到的是一个数。可以对它进行运算。