Advertisement
disk6969

business mode

Sep 24th, 2024 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. import telethon
  2. from telethon import TelegramClient, Button, events, types, functions, errors
  3.  
  4.  
  5. MEDIA_PATH = '...'
  6. BOT_TOKEN='...'
  7. bot = TelegramClient('bot', API_ID, API_HASH)
  8.  
  9. import logging
  10.  
  11. logging.basicConfig(
  12.     format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.INFO
  13. )
  14.  
  15.  
  16. from telethon.tl.functions import InvokeWithBusinessConnectionRequest
  17. from telethon.tl.types import UpdateBotNewBusinessMessage, UpdateBotEditBusinessMessage
  18. from telethon.tl.functions.messages import (
  19.     EditMessageRequest,
  20.     SendMessageRequest,
  21.     SendMediaRequest,
  22.     UploadMediaRequest,
  23. )
  24. from telethon.errors import UserMigrateError
  25.  
  26.  
  27. def parse_response(updates, peer):
  28.     for e in updates.updates:
  29.         if isinstance(e, (UpdateBotNewBusinessMessage, UpdateBotEditBusinessMessage)):
  30.             e.message._finish_init(
  31.                 bot, {i.id: i for i in updates.chats + updates.users}, peer
  32.             )
  33.             return e.message
  34.  
  35.  
  36. async def invoke_business(conn_id, req, chat_id):
  37.     await req.resolve(bot, telethon.utils)
  38.     req = InvokeWithBusinessConnectionRequest(conn_id, req)
  39.     result = await bot(req)
  40.     return parse_response(result, await bot.get_input_entity(chat_id))
  41.  
  42.  
  43. async def edit_businsess_message(conn_id, chat_id, msg_id, new_message, **kwargs):
  44.     message, entities = bot.parse_mode.parse(new_message)
  45.     return await invoke_business(
  46.         conn_id,
  47.         EditMessageRequest(
  48.             peer=chat_id, id=msg_id, message=message, entities=entities, **kwargs
  49.         ),
  50.         chat_id,
  51.     )
  52.  
  53.  
  54. async def send_business_message(conn_id, chat_id, message):
  55.     message, entities = bot.parse_mode.parse(message)
  56.     return await invoke_business(
  57.         conn_id,
  58.         SendMessageRequest(
  59.             peer=chat_id,
  60.             message=message,
  61.             entities=entities,
  62.         ),
  63.         chat_id,
  64.     )
  65.  
  66.  
  67. async def send_business_media(conn_id, chat_id, file, caption=None):
  68.     message, entities = bot.parse_mode.parse(caption) if caption else '', None
  69.     media = await bot(
  70.         UploadMediaRequest('me', (await bot._file_to_media(file))[1], conn_id)
  71.     )
  72.     return await invoke_business(
  73.         conn_id,
  74.         SendMediaRequest(
  75.             peer=chat_id,
  76.             media=media,
  77.             message=message,
  78.             entities=entities,
  79.         ),
  80.         chat_id,
  81.     )
  82.  
  83.  
  84. @bot.on(events.Raw(UpdateBotNewBusinessMessage))
  85. async def new_biz_message(event):
  86.     message = await send_business_message(
  87.         event.connection_id, event.message.chat_id, 'Hi! how can i help you?'
  88.     )
  89.     message = await send_business_media(
  90.         event.connection_id, event.message.chat_id, MEDIA_PATH
  91.     )
  92.     await edit_businsess_message(
  93.         event.connection_id,
  94.         event.message.chat_id,
  95.         message.id,
  96.         'Hi! how can i help you?',
  97.     )
  98.  
  99.  
  100. bot.start(bot_token=BOT_TOKEN)
  101. bot.run_until_disconnected()
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement