使用电报python API获取一条消息?

使用电报python API获取一条消息?,python,python-telegram-bot,Python,Python Telegram Bot,我想创建一个电报机器人,在看到命令/定义后,请求输入单词。 我想在bot请求后提取用户发送的单词。我该怎么做 import telegram from telegram.ext import Updater from telegram.ext import MessageHandler, Filters from telegram.ext import CommandHandler updater = Updater(token='******************') dispatcher

我想创建一个电报机器人,在看到命令/定义后,请求输入单词。 我想在bot请求后提取用户发送的单词。我该怎么做

import telegram
from telegram.ext import Updater
from telegram.ext import MessageHandler, Filters
from telegram.ext import CommandHandler

updater = Updater(token='******************')
dispatcher = updater.dispatcher

def define(bot, update):
    bot.send_message(chat_id=update.message.chat_id, text="Enter word")
    word = '''get content of following message'''
    definition = get_definition(word)
    bot.send_message(chat_id=update.message.chat_id, text=definiton)

definition_handler = CommandHandler('define', define)

dispatcher.add_handler(definition_handler)

updater.start_polling()
  • 首先,你需要图书馆
  • 然后,您需要在电报中添加@BotFather并遵循以下步骤。您需要获得bot令牌,它是您的bot的一组唯一的字母和数字,就像代码名一样。在您通过@BotFather注册了一个bot之后,它会给您令牌
事实上,令牌是创建所需机器人的唯一工具。像您这样的机器人的代码应该遵循相同的逻辑结构:

# -*- coding: utf-8 -*-
import telebot  # importing pyTelegramBotAPI library
import time
import sys

bot = telebot.Telebot(token='YOUR API TOKEN')  # supply your future bot with the token you have received

@bot.message_handler(commands=['define', 'Define'])
def echo_msg(message):
   echo = bot.send_message(chat_id=message.chat.it,
                           text='What word would you want me to extract, sir?')
   bot.register_next_step_handler(message=echo, callback=extract_msg)

def extract_msg(message):
   msg.append(message.text)
   print(msg)

def main_loop():
   bot.polling(none_stop=True)
   while True:
      time.sleep(1)

if __name__ == '__main__':
   try:
      main_loop()
   except KeyboardInterrupt:
      print(sys.stderr '\nExiting by user request'\n')
      sys.exit(0)

好的,每个bot都需要一个
消息处理程序来处理传入的信息

在您的例子中,它是一个命令,触发bot请求将单词提取到列表中。如果未定义
bot.register\u next\u step\u handler()
,此命令将根本不会执行任何操作(除非它要求输入单词)

函数
extract_msg()
附加用户编写的下一个单词,并将
msg
列表打印到控制台中

函数
main\u loop()
运行bot直到暂停,并在每次提取单词后使其空闲一秒钟。要停止bot,请按Ctrl+C



我希望这就足够了。下一步是跟踪键入
/define
/define
的人,并提取他/她的word请求。另外,最好使
msg
list更具描述性,或者实现完全不同的提取方法。这是一个简单的信息,几乎不适用于实践。

我在调用stderr时修复了一个错误:

# -*- coding: utf-8 -*-
import telebot  # importing pyTelegramBotAPI library
import time
import sys

bot = telebot.Telebot(token='YOUR API TOKEN')  # supply your future bot with the token you have received

@bot.message_handler(commands=['define', 'Define'])
def echo_msg(message):
   echo = bot.send_message(chat_id=message.chat.it,
                           text='What word would you want me to extract, sir?')
   bot.register_next_step_handler(message=echo, callback=extract_msg)

def extract_msg(message):
   msg.append(message.text)
   print(msg)

def main_loop():
   bot.polling(none_stop=True)
   while True:
      time.sleep(1)

if __name__ == '__main__':    try:
      main_loop()    except KeyboardInterrupt:
      print(sys.stderr('\nExiting by user request'\n'))
      sys.exit(0)

到目前为止你有没有尝试过任何代码?你的问题很广泛,请阅读@Sharad Bhat我注意到你已经发布了一些缺失的问题。请复习,以提高您的提问能力和获得所需答案的能力。还请记住,如果发生以下情况,账户可能会被禁止提问。问得好的问题最有可能吸引更好的答案。祝你好运