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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| import json from PY.tools.py import pyTools import datetime import time import sys import random import re import requests import asyncio import telegram from telegram import Update, InlineQueryResultArticle, InputTextMessageContent from telegram.ext import InlineQueryHandler, CommandHandler, ContextTypes, ApplicationBuilder, MessageHandler, filters import logging from PY.tools.py import pyTools
''' 仓库wiki:https://github.com/python-telegram-bot/python-telegram-bot/wiki/Introduction-to-the-API 官方教程(java):https://core.telegram.org/bots/tutorial#first-run '''
OPENAI_API = '' TELEGRAM_TOKEN = ''
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): await context.bot.send_message( chat_id=update.effective_chat.id, text="您好先生,我是Jarvis,您的tg助理!" )
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE): response_text = pyTools.openai_chat( update.message.text, OPENAI_API) await context.bot.send_message(chat_id=update.effective_chat.id, text=response_text)
async def caps(update: Update, context: ContextTypes.DEFAULT_TYPE): text_caps = ' '.join(context.args).upper() await context.bot.send_message(chat_id=update.effective_chat.id, text=text_caps)
async def inline_caps(update: Update, context: ContextTypes.DEFAULT_TYPE): query = update.inline_query.query if not query: return results = [] results.append( InlineQueryResultArticle( id=query.upper(), title='Cps', input_message_content=InputTextMessageContent(query.upper()) ) ) await context.bot.answer_inline_query(update.inline_query.id, results)
async def unknow(update: Update, context: ContextTypes.DEFAULT_TYPE): await context.bot.send_message(chat_id=update.effective_chat.id, text='这是嘛意思?给我整不明白了都')
if __name__ == '__main__': application = ApplicationBuilder().token(TELEGRAM_TOKEN).build() start_handler = CommandHandler('start', start) echo_handler = MessageHandler(filters.TEXT & ( ~filters.COMMAND), echo) caps_handler = CommandHandler('caps', caps) unknow_handler = MessageHandler(filters.COMMAND, unknow)
inline_caps_handler = InlineQueryHandler(inline_caps)
application.add_handler(start_handler) application.add_handler(echo_handler) application.add_handler(caps_handler) application.add_handler(inline_caps_handler) application.add_handler(unknow_handler) application.run_polling()
|