- 注册机器人 :使用Botfather 按照提示快速注册,会得到一串密钥 格式类似 7878875019:BAGQ9AihJyE5jmSoWMt4O1j1CQThjfwR0nk
"""
@author: JHC000abc@gmail.com
@file: TelegramBot.py
@time: 2025/3/30 14:55
@desc:
"""
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, CallbackContext
class TwitterBot:
"""
"""
def __init__(self, bot_token):
self.bot_token = bot_token
async def parse_functions(self, text):
"""
:param text:
:return:
"""
val = ""
try:
args = text.split(" ")
if len(args) < 2:
key = args.strip()
if key in ("show", "showb"):
print(f"收到命令:{key}")
else:
print("未知命令")
else:
key, val = args
val = val.strip().replace(" ", "")
print(f"收到命令:{key} value:{val}")
except Exception as e:
print("未知命令", traceback.format_exc())
return val
async def start(self, update: Update, context: CallbackContext) -> None:
"""
:param update:
:param context:
:return:
"""
await self.parse_functions(update.message.text)
msg = (f"这是一个*Twitter*配置机器人\n当前支持命令:`/add` `/del` `/show` `/web` `/sleep` \n"
f" `/addb` `/delb` `/showb` `/webb` `/sleepb`")
await context.bot.send_message(chat_id=update.effective_chat.id,
text=msg, parse_mode='Markdown')
return
def process(self):
application = ApplicationBuilder().token(self.bot_token).build()
application.add_handler(CommandHandler('start', self.start)
print("当前机器人已启动")
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == '__main__':
token = "7878875019:BAGQ9AihJyE5jmSoWMt4O1j1CQThjfwR0nk"
tb = TwitterBot(token)
tb.process()