本文整理汇总了Python中pyrogram.Client方法的典型用法代码示例。如果您正苦于以下问题:Python pyrogram.Client方法的具体用法?Python pyrogram.Client怎么用?Python pyrogram.Client使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyrogram
的用法示例。
在下文中一共展示了pyrogram.Client方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: begin_import
# 需要导入模块: import pyrogram [as 别名]
# 或者: from pyrogram import Client [as 别名]
def begin_import(config: dict):
"""Skeleton fucntion that creates client and import, write config"""
client = pyrogram.Client(
"media_downloader",
api_id=config["api_id"],
api_hash=config["api_hash"],
)
await client.start()
last_read_message_id = await process_messages(
client,
config["chat_id"],
config["last_read_message_id"],
config["media_types"],
config["file_formats"],
)
await client.stop()
config["last_read_message_id"] = last_read_message_id + 1
return config
示例2: __init__
# 需要导入模块: import pyrogram [as 别名]
# 或者: from pyrogram import Client [as 别名]
def __init__(self, listener):
super().__init__()
self.__listener = listener
self.__resource_lock = threading.RLock()
self.__name = ""
self.__gid = ''
self.__start_time = time.time()
self.__user_bot = Client(api_id=TELEGRAM_API,
api_hash=TELEGRAM_HASH,
session_name=USER_SESSION_STRING)
self.__user_bot.start()
self.__is_cancelled = False
示例3: __init__
# 需要导入模块: import pyrogram [as 别名]
# 或者: from pyrogram import Client [as 别名]
def __init__(self, config: Config):
self._config = config
self._client = pyrogram.Client(config.session_name, config.api_id, config.api_hash,
bot_token=config.token, sleep_threshold=0)
示例4: _play
# 需要导入模块: import pyrogram [as 别名]
# 或者: from pyrogram import Client [as 别名]
def _play(self, client: Client, message: Message):
state, args = self._get_state(message)
if state != "select":
return
self._set_state(message, False)
if message.text == "Cancel":
await message.reply("Cancelled", reply_markup=_remove)
return
# noinspection PyTupleAssignmentBalance
msg_id, filename, devices = args
try:
device = next(
device
for device in devices
if repr(device) == message.text
)
except StopIteration:
await message.reply("Wrong device", reply_markup=_remove)
return
await device.stop()
await device.play(f"http://{self._config.listen_host}:{self._config.listen_port}/stream/{msg_id}", filename)
await message.reply(f"Playing ID: {msg_id}", reply_markup=_remove)
# noinspection PyUnusedLocal
示例5: _new_document
# 需要导入模块: import pyrogram [as 别名]
# 或者: from pyrogram import Client [as 别名]
def _new_document(self, client: Client, message: Message):
devices = []
if self._config.upnp_enabled:
devices.extend(await UpnpDeviceFinder.find(self._config))
if self._config.chromecast_enabled:
# noinspection PyUnresolvedReferences
devices.extend(await ChromecastDeviceFinder.find(self._config))
if self._config.xbmc_enabled:
devices.extend(await XbmcDeviceFinder.find(self._config))
if self._config.vlc_enabled:
devices.extend(await VlcDeviceFinder.find(self._config))
if devices:
file_name = ""
for typ in named_media_types:
obj = getattr(message, typ)
if obj is not None:
file_name = obj.file_name
break
self._set_state(message, "select", message.message_id, file_name, devices.copy())
buttons = [[KeyboardButton(repr(device))] for device in devices]
buttons.append([KeyboardButton("Cancel")])
markup = ReplyKeyboardMarkup(buttons, one_time_keyboard=True)
await message.reply("Select a device", reply_markup=markup)
else:
await message.reply("Supported devices not found in the network", reply_markup=_remove)
示例6: main
# 需要导入模块: import pyrogram [as 别名]
# 或者: from pyrogram import Client [as 别名]
def main(api_id, api_hash):
""" generate StringSession for the current MemorySession"""
async with Client(
":memory:",
api_id=api_id,
api_hash=api_hash
) as app:
print(app.export_session_string())
示例7: download_media
# 需要导入模块: import pyrogram [as 别名]
# 或者: from pyrogram import Client [as 别名]
def download_media(
client: pyrogram.client.client.Client,
message: pyrogram.Message,
media_types: List[str],
file_formats: dict,
):
"""Download media from Telegram.
Parameters
----------
client: pyrogram.client.client.Client
Client to interact with Telegram APIs.
message: pyrogram.Message
Message object retrived from telegram.
media_types: list
List of strings of media types to be downloaded.
Ex : `["audio", "photo"]`
Supported formats:
* audio
* document
* photo
* video
* voice
file_formats: dict
Dictionary containing the list of file_formats
to be downloaded for `audio`, `document` & `video`
media types
Returns
-------
integer
message_id
"""
def _can_download(_type, file_formats, file_format):
if _type in ["audio", "document", "video"]:
allowed_formats: list = file_formats[_type]
if (
not file_format in allowed_formats
and allowed_formats[0] != "all"
):
return False
return True
if message.media:
for _type in media_types:
_media = getattr(message, _type, None)
if _media:
file_ref, file_name, file_format = await _get_media_meta(
_media, _type
)
if _can_download(_type, file_formats, file_format):
download_path = await client.download_media(
message, file_ref=file_ref, file_name=file_name
)
logger.info("Media downloaded - %s", download_path)
return message.message_id
示例8: process_messages
# 需要导入模块: import pyrogram [as 别名]
# 或者: from pyrogram import Client [as 别名]
def process_messages(
client: pyrogram.client.client.Client,
chat_id: str,
last_read_message_id: int,
media_types: List[str],
file_formats: dict,
) -> int:
"""Download media from Telegram.
Parameters
----------
client: pyrogram.client.client.Client
Client to interact with Telegram APIs.
chat_id: string
Id of the chat to download media from.
last_read_message_id: integer
Id of last message read from the conversational thread.
media_types: list
List of strings of media types to be downloaded.
Ex : `["audio", "photo"]`
Supported formats:
* audio
* document
* photo
* video
* voice
file_formats: dict
Dictionary containing the list of file_formats
to be downloaded for `audio`, `document` & `video`
media types
Returns
-------
integer
last_message_id
"""
message_ids = await asyncio.gather(
*[
download_media(client, message, media_types, file_formats)
async for message in client.iter_history(
chat_id, offset_id=last_read_message_id, reverse=True
)
]
)
last_message_id = max(message_ids, default=last_read_message_id)
return last_message_id