标签: python-telegram-bot

Telegram bot api键盘

我有Telegram Bot Api和"ReplyKeyboard"的问题.我正在使用Python 2.7并发送帖子请求:

TelegramAPI.post(TELEGRAM_URL + "sendMessage", data=dict(chat_id=CHAT_ID, text="", keyboard={'keyboard': keyboard, 'one_time_keyboard': False, 'resize_keyboard': True})
Run Code Online (Sandbox Code Playgroud)

这种格式的键盘:

[["A button"], ["B button"]]
Run Code Online (Sandbox Code Playgroud)

但在电报中我没有看到键盘.可能有什么问题?

python tornado telegram python-telegram-bot telegram-bot

7
推荐指数
1
解决办法
4518
查看次数

(python) Telegram bot-如何定期发送消息?

我对我的电报机器人进退两难。假设我必须创建一个函数来询问连接到机器人的每个用户,每周/每月一次,一个问题:

def check_the_week(bot, update):
reply_keyboard = [['YES', 'NO']]
bot.send_message(
    chat_id=update.message.chat_id,
    text=report,

    reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))  # sends the total nr of hours
update.reply_text("Did you report all you working hour on freshdesk for this week?",
                  ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))

if update.message.text == "YES":
    update.message.reply_text(text="Are you sure?",
                              reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))

    # Asks confirmation
    if update.message.text == "YES":
        update.message.reply_text(text="Thank you for reporting your working hours in time!")

    elif update.message.text == "NO":
        update.message.reply_text(text="Please, check you time reports and add missing")

elif update.message.text == "NO":
    update.message.reply_text(text="Please, check you time reports …
Run Code Online (Sandbox Code Playgroud)

python telegram python-telegram-bot telegram-bot

7
推荐指数
2
解决办法
2万
查看次数

处理用户在电报机器人中删除的消息

有什么方法可以在一对一聊天或机器人所属的群组中处理用户删除的消息?有编辑消息更新的方法,但没有删除消息的方法。

python-telegram-bot telegram-bot

7
推荐指数
1
解决办法
1473
查看次数

如何在电报python bot中保存照片?

我想写一个电报机器人来保存照片。这是我的代码,但它不起作用。我不知道我的问题是什么?

def image_handler(bot, update):
    file = bot.getFile(update.message.photo.file_id)
    print ("file_id: " + str(update.message.photo.file_id))
    file.download('image.jpg')

updater.dispatcher.add_handler(MessageHandler(Filters.photo, image_handler))
updater.start_polling()
updater.idle()
Run Code Online (Sandbox Code Playgroud)

请帮我解决我的问题。

photo python-3.x python-telegram-bot telegram-bot

7
推荐指数
2
解决办法
7695
查看次数

使用python-telegram-bot构建菜单的正确方法

我与python-telegram-botBotFather机器人合作并尝试构建嵌套菜单系统.例如,您有一个通用的机器人菜单

pic.1

您可以在其中选择"编辑机器人"并获取新的相应菜单

pic.2

可以选择返回上一个菜单.

我尝试用代码实现:

# main menu
def start(bot, update):
    menu_main = [[InlineKeyboardButton('Option 1', callback_data='m1')],
                 [InlineKeyboardButton('Option 2', callback_data='m2')],
                 [InlineKeyboardButton('Option 3', callback_data='m3')]]
    reply_markup = InlineKeyboardMarkup(menu_main)
    update.message.reply_text('Choose the option:', reply_markup=reply_markup)

# all other menus
def menu_actions(bot, update):
    query = update.callback_query

    if query.data == 'm1':
        # first submenu
        menu_1 = [[InlineKeyboardButton('Submenu 1-1', callback_data='m1_1')],
                  [InlineKeyboardButton('Submenu 1-2', callback_data='m1_2')]]
        reply_markup = InlineKeyboardMarkup(menu_1)
        bot.edit_message_text(chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              text='Choose the option:',
                              reply_markup=reply_markup)
    elif query.data == 'm2':
        # second submenu
        # first submenu
        menu_2 = [[InlineKeyboardButton('Submenu 2-1', callback_data='m2_1')],
                  [InlineKeyboardButton('Submenu …
Run Code Online (Sandbox Code Playgroud)

python bots menu telegram python-telegram-bot

7
推荐指数
2
解决办法
6443
查看次数

如何在 Python-telegram-bot 中使用 Jobqueue

我可以通过阅读文档非常轻松地创建一个机器人,但是 Jobqueue 并没有按照它所写的那样工作。该run_daily方法使用datetime.time对象在特定时间发送消息,但此代码既不执行发送消息的工作,也不显示任何错误。它只是继续运行

    import datetime
    from telegram import bot
    from telegram.ext import Updater
    def callback_minute(bot, job):
        bot.send_message(chat_id=475838704, text='PlEaSe wOrK!')

    def main():
        updater = Updater()
        bot = updater.bot
        job = updater.job_queue

        dispatcher = updater.dispatcher

        job.run_daily(callback_minute, time=datetime.time(6,33,00))

        updater.start_polling()
        updater.idle()

    if __name__ == '__main__':
        main()
Run Code Online (Sandbox Code Playgroud)

python job-queue python-3.x python-telegram-bot telegram-bot

7
推荐指数
1
解决办法
2万
查看次数

如何通过电报机器人发送 PIL 图像而不将其保存到文件

对于我的电报机器人(python-telegram-bot),我生成了一个 PIL.Image.Image 并且我想将它直接发送给用户。

有效的是从文件中将图像作为 bufferedReader 发送,但我不想保护图像。之后我不再需要它,我可能会同时生成很多不同的图像,所以保存有点麻烦。

bot.send_photo(chat_id=update.message.chat_id,
               photo=open(img_dir, 'rb'),
               caption='test',
               parse_mode=ParseMode.MARKDOWN)
Run Code Online (Sandbox Code Playgroud)

因为是我自己生成的,所以不能使用 URL 或 file_id。我认为有可能将图像转换为 bufferedReader,但我只设法从中获取了一个字节对象,这不起作用。

图像生成如下:

images = [Image.open(i) for i in dir_list]
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGBA', (total_width, max_height))

x_offset = 0
for im in images:
    new_im.paste(im, (x_offset, 0))
    x_offset += im.size[0]
return new_im                 # returns a PIL.Image.Image
Run Code Online (Sandbox Code Playgroud)

提前致谢:) 圣诞快乐

python type-conversion python-imaging-library python-3.x python-telegram-bot

7
推荐指数
1
解决办法
2628
查看次数

采样音频不会保留波(矢量)!

我做了一个Telegram机器人,它的工作之一就是从音频文件创建样本。现在,对于发送给它的大多数音频而言,样本都很好。像这样的东西:

在此处输入图片说明

但是,对于某些音频,样本看起来有些奇怪:

在此处输入图片说明

如您所见,此文件中的波未显示!(我可以向您保证声音不是空的)

为了创建示例,我使用pydub(谢谢James!)。这是我创建示例的部分:

song = AudioSegment.from_mp3('song.mp3')
sliced = song[start*1000:end*1000]
sliced.export('song.ogg', format='ogg', parameters=["-acodec", "libopus"])
Run Code Online (Sandbox Code Playgroud)

然后使用bot.send_voice方法发送样本。像这样

bot.send_voice(
    chat_id=update.message.chat.id,
    voice=open('song.ogg', 'rb'),
    caption=settings.caption,
    parse_mode=ParseMode.MARKDOWN,
    timeout=1000
)
Run Code Online (Sandbox Code Playgroud)

Telegram Bot API的文档说:

如果希望Telegram客户端将文件显示为可播放的语音消息,请使用此方法发送音频文件。为此,您的音频必须位于使用OPUS编码的.ogg文件中(其他格式可能以音频或文档的形式发送)。

这就是为什么在这行代码中:

sliced.export('song.ogg', format='ogg', parameters=["-acodec", "libopus"])
Run Code Online (Sandbox Code Playgroud)

我用过parameters=["-acodec", "libopus"]

谁能告诉我我在做什么错?提前致谢!

python audio pydub telegram python-telegram-bot

7
推荐指数
1
解决办法
195
查看次数

如何通过 API 从 Telegram 群组获取消息 - Python

我正在寻找某种方式来收听和捕捉电报 gropus 提供的新消息。

我还没有找到库或 API 以便在 python 中执行此操作。

有人有什么建议吗?

谢谢,

房车

python api chat telegram python-telegram-bot

7
推荐指数
2
解决办法
2万
查看次数

需要在 Python 上从 Telegram 下载语音消息

我开始开发一个与电报机器人相关的宠物项目。问题之一是,如何从机器人下载语音消息?

任务:需要从电报机器人下载音频文件并保存在项目文件夹中。

GetUpdates https://api.telegram.org/bot /getUpdates:

{"duration":2,"mime_type":"audio/ogg","file_id":"<file_id>","file_unique_id":"<file_unique_id>","file_size":8858}}}]}
Run Code Online (Sandbox Code Playgroud)


我检查了pyTelegramBotAPI文档,但没有找到确切如何下载文件的解释。

我根据文档创建了代码:

@bot.message_handler(content_types=['voice'])
def voice_processing(message):
    file_info = bot.get_file(message.voice.file_id)
    file = requests.get('https://api.telegram.org/file/bot{0}/{1}'.format(cfg.TOKEN, file_info.file_path))
Run Code Online (Sandbox Code Playgroud)
print(type(file), file)
------------------------------------------------------------
Output: <class 'requests.models.Response'>, <Response [200]>
Run Code Online (Sandbox Code Playgroud)


我还发现了一个例子,作者以块的形式下载音频。我不明白到底是怎么回事,但它使用了类似的功能

def read_chunks(chunk_size, bytes):
    while True:
        chunk = bytes[:chunk_size]
        bytes = bytes[chunk_size:]

        yield chunk

        if not bytes:
            break
Run Code Online (Sandbox Code Playgroud)

python python-requests telegram python-telegram-bot telegram-bot

7
推荐指数
1
解决办法
1万
查看次数