【问题标题】:TypeError: get_updates() missing 1 required positional argument: 'self'类型错误:get_updates() 缺少 1 个必需的位置参数:'self'
【发布时间】:2019-10-02 13:59:32
【问题描述】:

我正在尝试构建一个 python 电报机器人,但我不断收到此错误,我无法找到错误的来源... 详情:

我的班级:

class weed4us_bot():
    def __init__(self, config):
        self.token = self.read_token_from_config_file(config)
        self.base = 'https://api.telegram.org/bot{}/'.format(self.token)

    def get_updates(self, offset=None):
        url = self.base + 'getUpdates?timeout=100'
        if offset:
            url = url + '&offset={}'.format(offset + 1)
        r = requests.get(url)
        return json.loads(r.content)

    def send_massage(self, msg, chat_id):
        url = self.base + 'sendMessage?chat_id={}&text={}'.format(chat_id, msg)
        if msg is not None:
            requests.get(url)

def read_token_from_config_file(config):
    parser = cfg.ConfigParser()
    parser.read(config)
    return parser.get('creds', 'token')

我的主文件:

from main_bot import weed4us_bot as bot

update_id = None

def make_reply(msg):
    if msg is not None:
        reply = 'okay'
        return reply


while True:
    print('...')
    updates = bot.get_updates(offset=update_id)
    updates = updates['result']
    if updates:
        for item in updates:
            update_id = item['update_id']
            try:
                message = item['message']['text']
            except:
                message = None
            from_ = item['message']['from']['id']
            reply = make_reply(message)
            bot.send_massage(reply, From_)

我不断收到此错误:

TypeError: get_updates() 缺少 1 个必需的位置参数:'self'

有人可以帮帮我吗?

【问题讨论】:

    标签: python api bots telegram


    【解决方案1】:

    get_updates 是类weed4us_bot 的一个方法,所以如果你想调用这个方法,你需要在这个类的一个对象上调用它。所以首先需要创建一个类的对象:obj = weed4us_bot(),然后调用这个方法obj.get_updates(offset=update_id)

    还有第二种可能的方法来调用这个方法:weed4us_bot.get_updates(object, offset=update_id),但你仍然需要创建这个类的一个对象。

    您的错误出现在这一行:updates = bot.get_updates(offset=update_id)。要修复它,您可以首先创建类 weed4us_bot 的对象:bot_object = bot(some_config),然后在对象上调用方法:bot_object.get_updates(offset=update_id)。或者将 weed4us_bot 对象作为 self 传递。可以这样操作:bot.get_updates(bot(some_config), offset=update_id)

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2021-07-14
      • 2015-07-13
      • 2017-06-24
      • 2016-05-27
      • 2017-08-21
      • 2021-09-21
      • 2020-11-10
      • 2018-04-05
      相关资源
      最近更新 更多