V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
princelai
V2EX  ›  Telegram

有写过 telegram bot 的大佬吗,请问如何让机器人获取自己说的话?

  •  
  •   princelai · 2018-11-16 10:14:11 +08:00 · 3935 次点击
    这是一个创建于 1959 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码就是官方教程,我要实现的也很简单,就是另一个程序( tasker 或 ifttt )会把短信通过 webhook 传给 bot 的 api,然后 bot 会在和我的对话中发送这个短信。

    在第三个程序中,会从 LastMessage.mes 中获取信息,下面的代码会写成 flask 服务,让第三个程序从接口获取内容,这里面存储的应该是机器人说的最后一句话,现在的问题是,通过 api 只能拿到我说的话( update.message.text ),而无法拿到机器人自己说的话,不知道官方是否有这样的方法?

    import logging
    from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
    
    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
    TOKEN = "TOKEN"
    
    
    class LastMessage:
        mes = None
    
    
    def start(bot, update):
        bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
    
    
    def echo(bot, update):
        LastMessage.mes = update.message.text
        bot.send_message(chat_id=update.message.chat_id, text=update.message.text)
        print("Message is:{}".format(LastMessage.mes))
    
    
    updater = Updater(token=TOKEN)
    dispatcher = updater.dispatcher
    start_handler = CommandHandler('start', start)
    echo_handler = MessageHandler(Filters.text, echo)
    dispatcher.add_handler(echo_handler)
    dispatcher.add_handler(start_handler)
    
    updater.start_polling()
    

    在写 telegram 这个程序之前,我已经实现了微信+server 酱的相同功能,这套程序的问题是 server 酱推送来的信息是公众号的通知,使用 SYSTEM CONTEXT 和 ismpChat 可以获取到这个通知的到来,但是获取的 text 的内容是一个空列表,所以这个方法不行。

    3 条回复    2018-11-16 14:19:38 +08:00
    wql
        1
    wql  
       2018-11-16 12:58:11 +08:00 via Android
    telegram 里面机器人看不见机器人说的话……
    princelai
        2
    princelai  
    OP
       2018-11-16 13:42:45 +08:00
    @wql 也就是说自己也不存储自己说了什么是吗?
    orqzsf1
        3
    orqzsf1  
       2018-11-16 14:19:38 +08:00
    https://core.telegram.org/bots/api

    Type of the entity. Can be mention (@username), hashtag, cashtag, bot_command, url, email, phone_number, bold (bold text), italic (italic text), code (monowidth string), pre (monowidth block), text_link (for clickable text URLs), text_mention (for users without usernames)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3137 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 13:01 · PVG 21:01 · LAX 06:01 · JFK 09:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.