我试图建立一个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')
我的主文件:
^{pr2}$我不断地得到这个错误:
TypeError: get_updates() missing 1 required positional argument: 'self'
有人能帮帮我吗?在
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\u bot:bot_object = bot(some_config)
的对象,然后对对象调用方法:bot_object.get_updates(offset=update_id)
。或将weed4us_bot
对象作为自身传递。你可以这样做:bot.get_updates(bot(some_config), offset=update_id)
相关问题 更多 >
编程相关推荐