1

我正在使用 Spark(企业聊天)创建一个机器人,在 Python 中,我使用 PyGitHub 作为库。所以当我用机器人在我的房间里写“repos”时,他必须把我的repos列表发回给我。它适用于我的 github 个人帐户,但不适用于我的专业帐户。如果你能解释一下为什么?这是我的代码:

def gitTest(self, details, message):
        url = "https://enter-prise.com"
        token = "abcd"
        github = Github(token, base_url=url)
        for repo in github.get_organization("org").get_repos():
            self.answer(details.roomId, markdown=repo.name) 


Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/mflamant/Documents/bot/CiscoSparkPython/testbotforgithub/main.py", line 44, in Main
    bot.isRunnable()
  File "/home/mflamant/Documents/bot/CiscoSparkPython/testbotforgithub/utils/Compute.py", line 47, in isRunnable
    self.spark(message[0], message[1])
  File "/home/mflamant/Documents/bot/CiscoSparkPython/testbotforgithub/testbotforgithub.py", line 33, in spark
    return self.answer(details.roomId, markdown=self.gitTest(details, message))
  File "/home/mflamant/Documents/bot/CiscoSparkPython/testbotforgithub/testbotforgithub.py", line 56, in gitTest
    for repo in github.get_organization(adt).get_repos():
  File "/usr/local/lib/python2.7/dist-packages/PyGithub-1.35-py2.7.egg/github/Organization.py", line 539, in get_repos
    self.url + "/repos",
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

你能解释一下我的代码有什么问题吗?谢谢你

4

1 回答 1

2

如果gitTest是实例方法,则需要分配给属性self.url,而不仅仅是局部变量url。所以你的方法应该是这样的:

def gitTest(self, details, message):
    self.url = "https://enter-prise.com"
    self.token = "abcd"
    github = Github(token, base_url=url)
    for repo in github.get_organization("org").get_repos():
        self.answer(details.roomId, markdown=repo.name) 

这就是为什么您将引用self作为任何实例方法的第一个参数传递。

于 2017-10-17T16:13:39.347 回答