我目前正在通过电报 python 包使用电报机器人 API。到目前为止,我使用以下代码创建自定义键盘没有任何问题:
bot.sendMessage(chat_id, text="你现在在哪里?" "/取消中止",reply_markup=ReplyKeyboardMarkup([['Home', 'Office']], one_time_keyboard=True))
基本上上面会给我两个按钮,“主页”和“办公室”。
我现在正在尝试捕获用户的电话号码和位置,根据https://core.telegram.org/bots/api#keyboardbutton上的 Telegram API Bot 文档,这应该是可行的。但是,我真的很难弄清楚如何让它工作。
有任何想法吗?
我正在使用 制作 Telegram 机器人python-telegram-bot,我需要某种方式来接收语音消息。为此,我需要下载它们,为此,我必须获取它们的file_ids。但是,MessageHandler句柄......好吧,消息,并Handler给我一个NotImplementedError. 有没有办法获得file_id?
我安装了电报包。但是当我尝试运行简单的示例 echobot.py 时,出现错误:
Traceback (most recent call last):
File "echobot.py", line 8, in <module>
import telegram ImportError: No module named 'telegram'
Run Code Online (Sandbox Code Playgroud)
谁能帮我?
我使用 git 安装:
$ git clone https://github.com/python-telegram-bot/python-telegram-bot
Run Code Online (Sandbox Code Playgroud)
在这之后:
$ python -i
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import telegram
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'telegram'
Run Code Online (Sandbox Code Playgroud) 我正在发送InlineQueryResultArticle给客户,我想知道如何获得选定的结果及其数据(如 result_id,...)。
这是发送结果的代码:
token = 'Bot token'
bot = telegram.Bot(token)
updater = Updater(token)
dispatcher = updater.dispatcher
def get_inline_results(bot, update):
query = update.inline_query.query
results = list()
results.append(InlineQueryResultArticle(id='1000',
title="Book 1",
description='Description of this book, author ...',
thumb_url='https://fakeimg.pl/100/?text=book%201',
input_message_content=InputTextMessageContent(
'chosen book:')))
results.append(InlineQueryResultArticle(id='1001',
title="Book 2",
description='Description of the book, author...',
thumb_url='https://fakeimg.pl/300/?text=book%202',
input_message_content=InputTextMessageContent(
'chosen book:')
))
update.inline_query.answer(results)
inline_query_handler = InlineQueryHandler(get_inline_results)
dispatcher.add_handler(inline_query_handler)
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法on_inline_chosen(data)来获取所选项目的 id。(1000 or 1001对于上面的代码段),然后向用户发送适当的响应。
Telegram 的内联键盘是一个很棒的功能,有很多不同的用例。
内嵌按钮作为项目列表添加,如下所示:
inline_keyboard = [[InlineKeyboardButton(text="button", callback_data="button"),
InlineKeyboardButton(text="reset",callback_data="reset")]]
inline_keyboard_markup = InlineKeyboardMarkup(inline_keyboard)
update.message.reply_text("hi", reply_markup=inline_keyboard_markup)
Run Code Online (Sandbox Code Playgroud)
上面的代码添加了两个按钮,每个按钮的宽度为聊天屏幕的一半。
我知道对于普通的键盘按钮,有一个resize_keyboard可以以某种方式使用的参数。
我的问题是有没有办法调整内嵌按钮的大小?例如,使其全宽或四分之一宽。
我需要阅读应用程序中某些公共频道的消息,例如,它发生在https://tlgrm.ru/channels/tech。据我所知,该业务的机器人无法正常工作。您需要使用客户端api,但是与通道方法相连的每个地方都需要channel_id,但是我不知道从哪里获得,我只有通道名称,以及如何从中获得它的id我没有找到这样的方法。
如何获得频道名称的ID?
我正在开发一个电报机器人,并寻找确切的 URL 模式以在电报客户端中弹出“共享/选择组/发送到”对话框。
我在@pollbot 中看到了这个链接,为此我截取了以下屏幕截图。
更多细节:
tg协议 url: tg://resolve?domain=PollBot&startgroup=5148bed5f90678b93246464b3e132052。所以我试图通过 bot.sendMessage 重新发送这个 url。但事实证明 Telegram api 服务器不会解析tg://resolveurl。我正在尝试编写一个机器人,用户单击命令,将链接作为消息发送,然后机器人将链接添加到某个数据库。这是它的外观:
所以我认为我应该使用ConversationHandler. 这是我写的bot.py:
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters,
ConversationHandler)
from settings import BOT_TOKEN
import commands
def main():
updater = Updater(BOT_TOKEN, use_context=True)
dispatcher = updater.dispatcher
conversation = ConversationHandler(
entry_points=[
MessageHandler(
(Filters.command & Filters.regex("al_(.*)")),
commands.add_link
)
],
states={
commands.ADD_LINK: [
MessageHandler(Filters.entity("url"), commands.receive_link)
]
},
fallbacks=[]
)
dispatcher.add_handler(CommandHandler("search", commands.search))
dispatcher.add_handler(conversation)
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
命令在另一个名为的文件中commands.py:
from telegram.ext import ConversationHandler
ADD_LINK = range(1)
def receive_link(update, context):
bot = context.bot
url = update.message.text
chat_id …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个需要使用多个电报帐户登录的 python 脚本。我不想为每个帐户运行单独的脚本。我正在使用 TELETHON。我知道create_new_connection在 Telethon中有类似的东西,但我不知道它如何帮助我。有没有一种方法可以让我只使用一个 python 脚本并使用多个帐户登录..?(如果可能,请包含要在您的答案中使用的代码片段)
我正在使用 Python-Telegram-bot 制作电报机器人。我想让它向一个特定用户(在这种情况下是我自己)发送一条消息来选择一个选项。之后,它应该将该选项作为命令并照常工作。但是 30 分钟后……它应该向我发送相同的消息,让我像以前一样选择一个选项。我怎样才能让我工作?
def start(update: Update, context: CallbackContext) -> None:
user = update.message.from_user.username
print(user)
context.bot.send_message(chat_id=update.effective_chat.id, text= "Choose an option. ('/option1' , '/option 2', '/...')")
def main():
updater = Updater("<MY-BOT-TOKEN>", use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
我希望它在不获取 /start 命令(更新)的情况下运行。但在此之后......会有正常的功能会得到更新,30分钟后我想再次运行这个“开始”功能而没有得到更新。