Python 电报机器人发送带有按钮的消息

huangapple go评论100阅读模式
英文:

Python Telegram bot send message with buttons

问题

I'd like to send a message through telegram bot with buttons. Once button is pressed i need to know which button was that and then change the text that came with the button.

I've almost found out how to do things separately but i can't unite them. To send a message with buttons i need either to write /start or have to press a menu button. I need those buttons to appear after the message without user having to press anything.

This is a script that i've found in the official description with added functions to send a message

  1. #!/usr/bin/env python
  2. # pylint: disable=unused-argument, wrong-import-position
  3. # This program is dedicated to the public domain under the CC0 license.
  4. """
  5. Basic example for a bot that uses inline keyboards. For an in-depth explanation, check out
  6. https://github.com/python-telegram-bot/python-telegram-bot/wiki/InlineKeyboard-Example.
  7. """
  8. import logging
  9. import asyncio
  10. from telegram import __version__ as TG_VER
  11. try:
  12. from telegram import __version_info__
  13. except ImportError:
  14. __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
  15. if __version_info__ < (20, 0, 0, "alpha", 1):
  16. raise RuntimeError(
  17. f"This example is not compatible with your current PTB version {TG_VER}. To view the "
  18. f"{TG_VER} version of this example, "
  19. f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
  20. )
  21. from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup, Update
  22. from telegram.ext import ApplicationBuilder, Application, CallbackQueryHandler, CommandHandler, ContextTypes
  23. # Enable logging
  24. logging.basicConfig(
  25. format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
  26. )
  27. # set higher logging level for httpx to avoid all GET and POST requests being logged
  28. logging.getLogger("httpx").setLevel(logging.WARNING)
  29. logger = logging.getLogger(__name__)
  30. async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
  31. """Sends a message with three inline buttons attached."""
  32. keyboard = [
  33. [
  34. InlineKeyboardButton("Option 1", callback_data="1"),
  35. InlineKeyboardButton("Option 2", callback_data="2"),
  36. ],
  37. [InlineKeyboardButton("Option 3", callback_data="3")],
  38. ]
  39. reply_markup = InlineKeyboardMarkup(keyboard)
  40. await update.message.reply_text("Please choose:", reply_markup=reply_markup)
  41. async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
  42. """Parses the CallbackQuery and updates the message text."""
  43. query = update.callback_query
  44. # CallbackQueries need to be answered, even if no notification to the user is needed
  45. # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
  46. await query.answer()
  47. await query.edit_message_text(text=f"Selected option: {query.data}")
  48. async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
  49. """Displays info on how to use the bot."""
  50. await update.message.reply_text("Use /start to test this bot.")
  51. # using telegram.Bot
  52. async def send(chat, msg):
  53. await Bot('<TOKEN>').sendMessage(chat_id=chat, text=msg)
  54. # using ApplicationBuilder
  55. async def send_more(chat, msg):
  56. application = ApplicationBuilder().token('<TOKEN>').build()
  57. await application.bot.sendMessage(chat_id=chat, text=msg)
  58. def main() -> None:
  59. """Run the bot."""
  60. # Create the Application and pass it your bot's token.
  61. application = Application.builder().token("<TOKEN>").build()
  62. # asyncio.run(send_more('<CHAT_ID>', 'Hello there!'))
  63. application.add_handler(CommandHandler("start", start))
  64. application.add_handler(CallbackQueryHandler(button))
  65. application.add_handler(CommandHandler("help", help_command))
  66. asyncio.run(send('<CHAT_ID>', 'Hello there!'))
  67. # Run the bot until the user presses Ctrl-C
  68. application.run_polling(allowed_updates=Update.ALL_TYPES)
  69. if __name__ == "__main__":
  70. main()

The new code:

  1. #!/usr/bin/env python
  2. # pylint: disable=unused-argument, wrong-import-position
  3. # This program is dedicated to the public domain under the CC0 license.
  4. """
  5. Basic example for a bot that uses inline keyboards. For an in-depth explanation, check out
  6. https://github.com/python-telegram-bot/python-telegram-bot/wiki/InlineKeyboard-Example.
  7. """
  8. import logging
  9. from telegram import __version__ as TG_VER
  10. try:
  11. from telegram import __version_info__
  12. except ImportError:
  13. __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
  14. if __version_info__ < (20, 0, 0, "alpha", 1):
  15. raise RuntimeError(
  16. f"This example is not compatible with your current PTB version {TG_VER}. To view the "
  17. f"{TG_VER} version of this example, "
  18. f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
  19. )
  20. from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup, Update
  21. from telegram.ext import ApplicationBuilder, Application, CallbackQueryHandler, CommandHandler, ContextTypes
  22. # Enable logging
  23. logging.basicConfig(
  24. format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
  25. )
  26. # set higher logging level for httpx to avoid all GET and POST requests being logged
  27. logging.getLogger("httpx").setLevel(logging.WARNING)
  28. logger = logging.getLogger(__name__)
  29. prolong_1y = 0
  30. prolong_2y = 0
  31. noprolong = 0
  32. async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
  33. """Parses the CallbackQuery and updates the message text."""
  34. query = update.callback_query
  35. # CallbackQueries need to be answered, even if no notification to the user is needed
  36. # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
  37. await query.answer()
  38. global prolong_1y, prolong_2y, noprolong
  39. if "prolong_1y" in query:
  40. prolong_1y = 1
  41. prolong_2y = 0
  42. noprolong = 0
  43. elif "prolong_2y" in query:
  44. prolong_1y = 0
  45. prolong_2y = 1
  46. noprolong = 0
  47. elif "noprolong" in query:
  48. prolong_1y = 0
  49. prolong_2y = 0
  50. noprolong = 1
  51. await query.edit_message_text(text=f"Selected option: {query.data}")
  52. print(f"prolong_1y => {prolong_1y}, prolong_2y => {prolong_2y} and noprolong => {noprolong}")
  53. # using telegram.Bot
  54. async def send(chat, msg, reply_markup):
  55. await Bot('<TOKEN>').sendMessage(chat_id=chat, text=msg, reply_markup=reply_markup)
  56. async def post_init(application:
  57. <details>
  58. <summary>英文:</summary>
  59. i&#39;d like to send a message through telegram bot with buttons. Once button is pressed i need to know which button was that and then change the text that came with the button.
  60. I&#39;ve almost found out how to do things separately but i can&#39;t unite them. To send a message with buttons i need either to write /start or have to press a menu button. I need those buttons to appear after the message without user having to press anything.
  61. This is a script that i&#39;ve found in the official description with added functions to send a message
  62. #!/usr/bin/env python
  63. # pylint: disable=unused-argument, wrong-import-position
  64. # This program is dedicated to the public domain under the CC0 license.
  65. &quot;&quot;&quot;
  66. Basic example for a bot that uses inline keyboards. For an in-depth explanation, check out
  67. https://github.com/python-telegram-bot/python-telegram-bot/wiki/InlineKeyboard-Example.
  68. &quot;&quot;&quot;
  69. import logging
  70. import asyncio
  71. from telegram import __version__ as TG_VER
  72. try:
  73. from telegram import __version_info__
  74. except ImportError:
  75. __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
  76. if __version_info__ &lt; (20, 0, 0, &quot;alpha&quot;, 1):
  77. raise RuntimeError(
  78. f&quot;This example is not compatible with your current PTB version {TG_VER}. To view the &quot;
  79. f&quot;{TG_VER} version of this example, &quot;
  80. f&quot;visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html&quot;
  81. )
  82. from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup, Update
  83. from telegram.ext import ApplicationBuilder, Application, CallbackQueryHandler, CommandHandler, ContextTypes
  84. # Enable logging
  85. logging.basicConfig(
  86. format=&quot;%(asctime)s - %(name)s - %(levelname)s - %(message)s&quot;, level=logging.INFO
  87. )
  88. # set higher logging level for httpx to avoid all GET and POST requests being logged
  89. logging.getLogger(&quot;httpx&quot;).setLevel(logging.WARNING)
  90. logger = logging.getLogger(__name__)
  91. async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -&gt; None:
  92. &quot;&quot;&quot;Sends a message with three inline buttons attached.&quot;&quot;&quot;
  93. keyboard = [
  94. [
  95. InlineKeyboardButton(&quot;Option 1&quot;, callback_data=&quot;1&quot;),
  96. InlineKeyboardButton(&quot;Option 2&quot;, callback_data=&quot;2&quot;),
  97. ],
  98. [InlineKeyboardButton(&quot;Option 3&quot;, callback_data=&quot;3&quot;)],
  99. ]
  100. reply_markup = InlineKeyboardMarkup(keyboard)
  101. await update.message.reply_text(&quot;Please choose:&quot;, reply_markup=reply_markup)
  102. async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -&gt; None:
  103. &quot;&quot;&quot;Parses the CallbackQuery and updates the message text.&quot;&quot;&quot;
  104. query = update.callback_query
  105. # CallbackQueries need to be answered, even if no notification to the user is needed
  106. # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
  107. await query.answer()
  108. await query.edit_message_text(text=f&quot;Selected option: {query.data}&quot;)
  109. async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -&gt; None:
  110. &quot;&quot;&quot;Displays info on how to use the bot.&quot;&quot;&quot;
  111. await update.message.reply_text(&quot;Use /start to test this bot.&quot;)
  112. # using telegram.Bot
  113. async def send(chat, msg):
  114. await Bot(&#39;&lt;TOKEN&gt;&#39;).sendMessage(chat_id=chat, text=msg)
  115. # using ApplicationBuilder
  116. async def send_more(chat, msg):
  117. application = ApplicationBuilder().token(&#39;&lt;TOKEN&gt;&#39;).build()
  118. await application.bot.sendMessage(chat_id=chat, text=msg)
  119. def main() -&gt; None:
  120. &quot;&quot;&quot;Run the bot.&quot;&quot;&quot;
  121. # Create the Application and pass it your bot&#39;s token.
  122. application = Application.builder().token(&quot;&lt;TOKEN&gt;&quot;).build()
  123. # asyncio.run(send_more(&#39;&lt;CHAT_ID&gt;&#39;, &#39;Hello there!&#39;))
  124. application.add_handler(CommandHandler(&quot;start&quot;, start))
  125. application.add_handler(CallbackQueryHandler(button))
  126. application.add_handler(CommandHandler(&quot;help&quot;, help_command))
  127. asyncio.run(send(&#39;&lt;CHAT_ID&gt;&#39;, &#39;Hello there!&#39;))
  128. # Run the bot until the user presses Ctrl-C
  129. application.run_polling(allowed_updates=Update.ALL_TYPES)
  130. if __name__ == &quot;__main__&quot;:
  131. main()
  132. &lt;&lt;&lt; EDIT &gt;&gt;&gt;
  133. The new code:
  134. #!/usr/bin/env python
  135. # pylint: disable=unused-argument, wrong-import-position
  136. # This program is dedicated to the public domain under the CC0 license.
  137. &quot;&quot;&quot;
  138. Basic example for a bot that uses inline keyboards. For an in-depth explanation, check out
  139. https://github.com/python-telegram-bot/python-telegram-bot/wiki/InlineKeyboard-Example.
  140. &quot;&quot;&quot;
  141. import logging
  142. from telegram import __version__ as TG_VER
  143. try:
  144. from telegram import __version_info__
  145. except ImportError:
  146. __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
  147. if __version_info__ &lt; (20, 0, 0, &quot;alpha&quot;, 1):
  148. raise RuntimeError(
  149. f&quot;This example is not compatible with your current PTB version {TG_VER}. To view the &quot;
  150. f&quot;{TG_VER} version of this example, &quot;
  151. f&quot;visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html&quot;
  152. )
  153. from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup, Update
  154. from telegram.ext import ApplicationBuilder, Application, CallbackQueryHandler, CommandHandler, ContextTypes
  155. # Enable logging
  156. logging.basicConfig(
  157. format=&quot;%(asctime)s - %(name)s - %(levelname)s - %(message)s&quot;, level=logging.INFO
  158. )
  159. # set higher logging level for httpx to avoid all GET and POST requests being logged
  160. logging.getLogger(&quot;httpx&quot;).setLevel(logging.WARNING)
  161. logger = logging.getLogger(__name__)
  162. prolong_1y = 0
  163. prolong_2y = 0
  164. noprolong = 0
  165. async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -&gt; None:
  166. &quot;&quot;&quot;Parses the CallbackQuery and updates the message text.&quot;&quot;&quot;
  167. query = update.callback_query
  168. # CallbackQueries need to be answered, even if no notification to the user is needed
  169. # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
  170. await query.answer()
  171. global prolong_1y, prolong_2y, noprolong
  172. if &quot;prolong_1y&quot; in query:
  173. prolong_1y = 1
  174. prolong_2y = 0
  175. noprolong = 0
  176. elif &quot;prolong_2y&quot; in query:
  177. prolong_1y = 0
  178. prolong_2y = 1
  179. noprolong = 0
  180. elif &quot;noprolong&quot; in query:
  181. prolong_1y = 0
  182. prolong_2y = 0
  183. noprolong = 1
  184. await query.edit_message_text(text=f&quot;Selected option: {query.data}&quot;)
  185. print(f&quot;prolong_1y =&gt; {prolong_1y}, prolong_2y =&gt; {prolong_2y} and noprolong =&gt; {noprolong}&quot;)
  186. # using telegram.Bot
  187. async def send(chat, msg, reply_markup):
  188. await Bot(&#39;&lt;TOKEN&gt;&#39;).sendMessage(chat_id=chat, text=msg, reply_markup=reply_markup)
  189. async def post_init(application: Application) -&gt; None:
  190. &quot;&quot;&quot;Sends a message with three inline buttons attached.&quot;&quot;&quot;
  191. keyboard = [
  192. [
  193. InlineKeyboardButton(&quot;Option 1&quot;, callback_data=&quot;prolong_1y&quot;),
  194. InlineKeyboardButton(&quot;Option 2&quot;, callback_data=&quot;prolong_2y&quot;),
  195. ],
  196. [InlineKeyboardButton(&quot;Option 3&quot;, callback_data=&quot;noprolong&quot;)],
  197. ]
  198. reply_markup = InlineKeyboardMarkup(keyboard)
  199. await send(&#39;&lt;CHAT_ID&gt;&#39;, &#39;Hello there!&#39;, reply_markup)
  200. def main() -&gt; None:
  201. &quot;&quot;&quot;Run the bot.&quot;&quot;&quot;
  202. # Create the Application and pass it your bot&#39;s token.
  203. application = Application.builder().token(&quot;&lt;TOKEN&gt;&quot;).post_init(post_init).build()
  204. application.add_handler(CallbackQueryHandler(button))
  205. # Run the bot until the user presses Ctrl-C
  206. application.run_polling(allowed_updates=Update.ALL_TYPES)
  207. if __name__ == &quot;__main__&quot;:
  208. main()
  209. The error that i receive after pressing on either Choice button:
  210. 2023-07-19 13:50:13,278 - apscheduler.scheduler - INFO - Scheduler started
  211. 2023-07-19 13:50:13,278 - telegram.ext.Application - INFO - Application started
  212. 2023-07-19 13:50:16,557 - telegram.ext.Application - ERROR - No error handlers are registered, logging exception.
  213. Traceback (most recent call last):
  214. File &quot;/home/admin2/.local/lib/python3.11/site-packages/telegram/ext/_application.py&quot;, line 1173, in process_update
  215. await coroutine
  216. File &quot;/home/admin2/.local/lib/python3.11/site-packages/telegram/ext/_basehandler.py&quot;, line 141, in handle_update
  217. return await self.callback(update, context)
  218. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  219. File &quot;/media/smb_general/Адміністрування/Source/ElReports/elreports/inlinekeyboard.py&quot;, line 50, in button
  220. if &quot;prolong_1y&quot; in query:
  221. ^^^^^^^^^^^^^^^^^^^^^
  222. File &quot;/home/admin2/.local/lib/python3.11/site-packages/telegram/_telegramobject.py&quot;, line 247, in __getitem__
  223. return getattr(self, item)
  224. ^^^^^^^^^^^^^^^^^^^
  225. TypeError: attribute name must be string, not &#39;int&#39;
  226. &lt;&lt;&lt; EDIT 2 &gt;&gt;&gt;
  227. Silly me, changed query to query.data now everything works!
  228. </details>
  229. # 答案1
  230. **得分**: 2
  231. 如果你想在`send`中发送的消息中附加按钮只需使用`send_message`的[相应参数](https://docs.python-telegram-bot.org/en/stable/telegram.bot.html#telegram.Bot.send_message.params.reply_markup)即可。请注意,`start`中使用的`message.reply_text`仅是该方法的快捷方式。
  232. 此外你无需在`send`中手动初始化一个机器人也无需通过`asyncio.run`手动运行该方法我建议改为使用`post_init`它允许作为`application.run_polling`的启动逻辑的一部分运行自定义逻辑
  233. 免责声明我目前是`python-telegram-bot`的维护者
  234. <details>
  235. <summary>英文:</summary>
  236. If you want to attach buttons to the message sent in `send`, you can just use the [corresponding parameter of `send_message`](https://docs.python-telegram-bot.org/en/stable/telegram.bot.html#telegram.Bot.send_message.params.reply_markup) for that. Not that [`message.reply_text`](https://docs.python-telegram-bot.org/en/stable/telegram.message.html#telegram.Message.reply_text) as used in `start` is just a shortcut for that method.
  237. Moreover, you don&#39;t need to manually initialize a bot in `send` and to manually run that method via `asyncio.run`. I recommend to instead make use of [`post_init`](https://docs.python-telegram-bot.org/en/stable/telegram.ext.applicationbuilder.html#telegram.ext.ApplicationBuilder.post_init) which allows to run a custom logic as part of the startup logic of `application.run_polling`.
  238. ---
  239. Disclaimer: I&#39;m currently the maintainer of `python-telegram-bot`.
  240. </details>

huangapple
  • 本文由 发表于 2023年7月17日 22:19:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定