diff --git a/.github/workflows/pyright.yml b/.github/workflows/pyright.yml new file mode 100644 index 0000000..4c648b4 --- /dev/null +++ b/.github/workflows/pyright.yml @@ -0,0 +1,38 @@ +name: Pyright Lint + +on: + push: + branches: + - beta + pull_request: + paths: + - "nonebot/**" + - "tests/**" + - ".github/actions/setup-python/**" + - ".github/workflows/pyright.yml" + - "pyproject.toml" + - "pdm.lock" + +jobs: + pyright: + name: Pyright Lint + runs-on: ubuntu-latest + concurrency: + group: pyright-${{ github.ref }} + cancel-in-progress: true + strategy: + fail-fast: false + + steps: + - uses: actions/checkout@v4 + + - name: Setup Python environment + uses: ./.github/actions/setup-python + with: + no-root: true + + - name: Install dependencies + run: pdm install -G tests -G nonebot + + - name: Run Pyright + uses: jakebailey/pyright-action@v2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 0080830..b7f960e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -215,7 +215,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 支持多个机器人同时在线 [Unreleased]: https://github.com/nonebot/adapter-telegram/compare/v0.1.0b16...HEAD -[0.1.0-beta.15]: https://github.com/nonebot/adapter-telegram/compare/v0.1.0b15...v0.1.0b16 +[0.1.0-beta.16]: https://github.com/nonebot/adapter-telegram/compare/v0.1.0b15...v0.1.0b16 [0.1.0-beta.15]: https://github.com/nonebot/adapter-telegram/compare/v0.1.0b14...v0.1.0b15 [0.1.0-beta.14]: https://github.com/nonebot/adapter-telegram/compare/v0.1.0b13...v0.1.0b14 [0.1.0-beta.13]: https://github.com/nonebot/adapter-telegram/compare/v0.1.0b12...v0.1.0b13 diff --git a/example/inline.py b/example/inline.py index 420487e..0ec95c3 100644 --- a/example/inline.py +++ b/example/inline.py @@ -17,36 +17,6 @@ ) -@on_command("inline").handle() -async def _(bot: Bot, event: MessageEvent): - await bot.send( - event, - "Hello InlineKeyboard !", - reply_markup=InlineKeyboardMarkup( - inline_keyboard=[ - [ - InlineKeyboardButton( - text="Nonebot Adapter Telegram", - url="https://github.com/nonebot/adapter-telegram", - ) - ], - [ - InlineKeyboardButton( - text="Telegram Bot API", - url="https://core.telegram.org/bots/api", - ) - ], - [ - InlineKeyboardButton( - text="Say hello to me", - callback_data="hello", - ) - ], - ] - ), - ) - - @on("inline").handle() async def _(bot: Bot, event: InlineQueryEvent): await bot.answer_inline_query( @@ -58,6 +28,28 @@ async def _(bot: Bot, event: InlineQueryEvent): input_message_content=InputTextMessageContent( message_text="Hello InlineQuery !" ), + reply_markup=InlineKeyboardMarkup( + inline_keyboard=[ + [ + InlineKeyboardButton( + text="Nonebot Adapter Telegram", + url="https://github.com/nonebot/adapter-telegram", + ) + ], + [ + InlineKeyboardButton( + text="Telegram Bot API", + url="https://core.telegram.org/bots/api", + ) + ], + [ + InlineKeyboardButton( + text="Say hello to me", + callback_data="hello", + ) + ], + ] + ), ), ], ) diff --git a/nonebot/adapters/telegram/adapter.py b/nonebot/adapters/telegram/adapter.py index d0e1b1f..e5ec273 100644 --- a/nonebot/adapters/telegram/adapter.py +++ b/nonebot/adapters/telegram/adapter.py @@ -187,7 +187,7 @@ async def process_input_file(file: Union[InputFile, str]) -> Optional[str]: media.media = f"attach://{filename}" # 对修改消息媒体消息的处理 elif api == "editMessageMedia": - media: Iterable[InputMedia] = data["media"] + media: InputMedia = data["media"] filename = await process_input_file(media.media) if filename: media.media = f"attach://{filename}" diff --git a/nonebot/adapters/telegram/bot.py b/nonebot/adapters/telegram/bot.py index 727528c..02bfad7 100644 --- a/nonebot/adapters/telegram/bot.py +++ b/nonebot/adapters/telegram/bot.py @@ -116,7 +116,7 @@ def __build_entities_form_msg( ( [ MessageEntity( - type=entity.type, + type=entity.type, # type: ignore offset=sum(map(len, message[:i])), length=len(entity.data["text"]), url=entity.data.get("url"), @@ -171,7 +171,7 @@ async def send_to( # 单个 segment if isinstance(message, MessageSegment): - if message.is_text(): + if isinstance(message, Entity): return await self.send_message( chat_id=chat_id, message_thread_id=message_thread_id, @@ -206,21 +206,31 @@ async def send_to( reply_parameters = ReplyParameters(**message["reply", 0].data) message = message.exclude("reply") - entities = Message(x for x in message if isinstance(x, Entity)) - files = Message( + entities = [x for x in message if isinstance(x, Entity)] + files = [ x for x in message - if isinstance(x, File) and not isinstance(message, UnCombinFile) - ) - others = Message( + if isinstance(x, File) and not isinstance(x, UnCombinFile) + ] + others = [ x for x in message - if not (isinstance(x, (Entity, File))) or isinstance(message, UnCombinFile) - ) - - # 如果只能单独发送的消息段和其他消息段在一起,那么抛出错误 - if others and (len(others) > 1 or files or entities): - raise ApiNotAvailable + if not (isinstance(x, (Entity, File))) or isinstance(x, UnCombinFile) + ] + + if others: + # 如果只能单独发送的消息段和其他消息段在一起,那么抛出错误 + if len(others) > 1 or files or entities: + raise ApiNotAvailable + other = others[0] + return await self.call_api( + f"send_{other.type}", + chat_id=chat_id, + message_thread_id=message_thread_id, + **other.data, + reply_parameters=reply_parameters, + **kwargs, + ) # 发送纯文本消息 if not files: diff --git a/nonebot/adapters/telegram/event.py b/nonebot/adapters/telegram/event.py index 54e4b8a..117e184 100644 --- a/nonebot/adapters/telegram/event.py +++ b/nonebot/adapters/telegram/event.py @@ -673,6 +673,8 @@ def get_event_description(self) -> str: class CallbackQueryEvent(InlineEvent, CallbackQuery): + chat: Chat = Field(default=None) + @overrides(Event) def get_event_name(self) -> str: return "inline.callback_query" diff --git a/nonebot/adapters/telegram/message.py b/nonebot/adapters/telegram/message.py index cb631f8..0bce2eb 100644 --- a/nonebot/adapters/telegram/message.py +++ b/nonebot/adapters/telegram/message.py @@ -67,7 +67,7 @@ def poll(question: str, options: List[str]) -> "MessageSegment": def dice( emoji: Literal["🎲", "🎯", "🏀", "⚽", "🎳", "🎰"] = "🎲" ) -> "MessageSegment": - return MessageSegment("dice", {"question": emoji}) + return MessageSegment("dice", {"emoji": emoji}) @staticmethod def chat_action( @@ -109,10 +109,6 @@ def game(game_short_name: str): class Reply(MessageSegment): - @overrides(BaseMessageSegment) - def is_text(self) -> bool: - return False - @staticmethod def reply(message_id: int, chat_id: Optional[Union[int, str]] = None, **kargs): return Reply("reply", {"message_id": message_id, "chat_id": chat_id, **kargs})