我正在使用python-telegram-bot图书馆。我想跟踪用户的实时位置,但我不知道该怎么做。我尝试使用作业队列:
def notification(bot, job):
updates = bot.get_updates()
print([u.message.location for u in updates])
# Add job to queue
job = job_queue.run_repeating(notification, 10, 1, context=chat_id)
chat_data['job'] = job
Run Code Online (Sandbox Code Playgroud)
但更新无效。我想每 1 分钟跟踪一次位置。
我已将我的机器人添加到群聊中,现在对于一些命令,我只需要向组管理员授予访问权限,那么是否可以识别消息发送者是否是组管理员?我正在使用 python-telegram-bot 库
我有一个关于用 python 构建电报机器人的问题。如何在 python-telegram-bot 中获取用户的输入?
例如,字典机器人,如何从用户那里获取单词?
我正在学习如何以用户身份登录时使用电视马拉松。目前我在创建内联按钮时遇到一些麻烦。我的代码是:
@register(outgoing=True, pattern="^\.button(?: |$)(.*)")
async def buttontest(test):
await test.client.send_message(test.chat_id, 'click me', buttons=[Button.inline('Test', 'test-return')])
Run Code Online (Sandbox Code Playgroud)
当我使用它时,我只看到一条“单击我”消息,没有按钮。作为用户使用 telethon 时按钮是否可用?
当我发出命令 /screen 时,我试图将计算机上的照片发送到我的电报机器人聊天室。但是,我似乎无法让它发挥作用,我错过了什么?
这是我正在使用的文件,抱歉它有点长,但我想展示所有内容,因为也许我在某个地方遗漏了一些东西。从 ###### HERE I WANNA TRY TO SEND THE PHOTO ###### 行查看,这样您就可以看到我在哪里定义 send_photo 部分。
import os
import bot
import schedule
import random
from time import sleep
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Python-Telegram-Bot 发送消息而不等待用户响应,但无法让它工作,并且没有收到任何错误。
Code:
def echo(context):
context.bot.send_message(chat_id=-516017547, text='test')
#chat_id is the group id I found by going to:
#https://api.telegram.org/bot<MY_API>/getUpdates
def main():
updater = Updater(API)
dp = updater.dispatcher
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
dp.add_handler(echo_handler)
updater.start_polling()
updater.idle()
main()
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我们如何将context.argsand传递context给另一个函数,在这种情况下callback_search_msgs
def search_msgs(update, context):
print('In TG, args', context.args)
context.job_queue.run_once(callback_search_msgs, 1, context=context, job_kwargs={'keys': context.args})
def callback_search_msgs(context, keys):
print('Args', keys)
chat_id = context.job.context
print('Chat ID ', chat_id)
def main():
updater = Updater(token, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("search_msgs",search_msgs, pass_job_queue=True,
pass_user_data=True))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud) 我在尝试向特定电报频道发送消息时遇到以下错误:
TimedOut: Timed out
The read operation timed out
Run Code Online (Sandbox Code Playgroud)
我从 python-telegram-bot 使用的方法是send_message。
尽管我的机器人收到此错误,但它仍然将消息发送到通道,并且因为我没有捕获该异常,消息中的所有数据都丢失了,但我确实需要在特定时间段后从该通道中删除我的消息。
即使超时,机器人仍发送消息,这可以吗?如何防止这种情况再次发生或在发送后将此类消息从频道中删除?
我在构建机器人方面相当新,所以我创建了一个非常简单的 Telegram 机器人,它工作得很好,但无法弄清楚如何让机器人在启动 /start_auto 命令时每 n 分钟或 n 小时发送消息。
我使用 while 循环做了一个解决方法,但它看起来很愚蠢,并且在循环期间用户将无法与机器人就其他主题进行交互。
我希望用户能够通过 /start_auto 和 /stop_auto 等命令启动和停止此计划任务。
我知道还有许多其他已回答的与该主题相关的问题,但似乎没有一个问题适用于我的代码。
import logging
import os
import time
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
logger = logging.getLogger(__name__)
PORT = int(os.environ.get('PORT', '8443'))
def start(update, context):
"""Sends a message when the command /start is issued."""
update.message.reply_text('Hi!')
def help(update, context):
"""Sends a message when the command /help is issued."""
update.message.reply_text('Help!')
def start_auto(update, context):
"""Sends a message when the command /start_auto is issued."""
n = 0
while …Run Code Online (Sandbox Code Playgroud) python scheduled-tasks telegram python-telegram-bot telegram-bot
我试过:
但错误不断出现,告诉我 python-telegram-bot 没有提供这样的“telegram.ext”包。但是,我的带有 python 插件的文本编辑器能够在我的环境中找到 telegram.ext 包。我还尝试在我创建的虚拟环境上直接使用命令提示符,但仍然不起作用。互联网上的其他帖子似乎也面临同样的问题,但没有任何解决方案。