2016-08-27 61 views
1

我使用slackclient使用下面的Python代碼測試鬆弛API正常工作:slackclient應用程序不PyInstaller工作後,建立但蟒蛇CMDLINE內

# pip install slackclient to install SlackClient library 
from slackclient import SlackClient 
import json 
import time 


def test_slack(sc): 
    # use for debugging 
    print("Testing API") 
    print(80 * "=") 
    print (sc.api_call("api.test")) 


def get_channel_info(sc, channel): 
    # get info about the channel 
    print("Getting Channel Info") 
    print(80 * "=") 
    print (sc.api_call("channels.info", channel=channel)) 


def get_list_of_channels(sc): 
    print("Getting List of Channels") 
    print(80 * "=") 
    # get list of channels 
    channels = sc.api_call("channels.list") 
    channels = json.dumps(channels) 
    channels = json.loads(str(channels)) 
    print channels 
    return channels 


def display_channels(channels): 
    print("Display Channels") 
    print(80 * "=") 
    for i in channels['channels']: 
     print("Channel:", i['name'], i['id']) 


def post_message(sc, text, channel, icon_url, username): 
    print("Posting Message to Slack") 
    print(80 * "=") 
    # post a message into the #general channel 
    print (sc.api_call("chat.postMessage", channel=channel, text=text, username=username, icon_url=icon_url, 
         unfurl_links="true")) 


def get_users(sc): 
    print("Get Users") 
    print(80 * "=") 
    # call the users.list api call and get list of users 
    users = (sc.api_call("users.list")) 
    print users 
    users = json.dumps(users) 
    users = json.loads(str(users)) 
    return users 


def display_users(sc, users): 
    print("User List") 
    print(80 * "=") 
    # display active users 
    for i in users['members']: 
     print users 
     # don't display slackbot 
     if i['profile']['real_name'] != "slackbot": 
      # don't display deleted users 
      if not i['deleted']: 
       # display real name 
       print (i['profile']['real_name']) 


def main(): 
    # define variables 
    token = "HERE YOUR SLACK TESTER TOKEN" 
    channel = "general" 
    username = "moderator" 
    icon_url = "" 
    # connect to Slack 
    sc = SlackClient(token) 
    # test slack 
    test_slack(sc) 
    # get channel info 
    get_channel_info(sc, channel) 
    # get list of channels 
    channels = get_list_of_channels(sc) 
    # display channels 
    display_channels(channels) 
    # post message 
    post_message(sc, "Testing 123 testing 123.", channel, icon_url, username) 
    # get users 
    users = get_users(sc) 
    # display users 
    display_users(sc, users) 

    if sc.rtm_connect(): 
     while True: 
      for slack_message in sc.rtm_read(): 
       print slack_message 
       message = slack_message.get("text") 
       user = slack_message.get("user") 
       if not message or not user: 
        continue 
       print "User: ", user, message 
      time.sleep(3) 
    else: 
     print "Connection Failed, invalid token?" 

main() 

這將運行從內PyCharm環境或從細python命令行。 用PyInstaller構建一個Windows EXE後,appication不能再連接,因爲rtm_connect()總是返回一個False。令牌或其他任何未被更改的。

有關如何調試此任何建議或任何想法可能導致連接問題?

的PyInstaller腳本:

# -*- mode: python -*- 

import os 
from os.path import join 

datas = [] 

# list of modules to exclude from analysis 
excludes = ['Tkinter', '_tkinter', 'twisted', 'pygments'] 

# list of hiddenimports 
hiddenimports = [] 

# binary data 
bin_tocs = [] 

# assets 
tocs = [] 

a = Analysis(['slacktest.py'], 
      pathex=[os.getcwd()], 
      binaries=None, 
      datas=datas, 
      hiddenimports=hiddenimports, 
      hookspath=[], 
      runtime_hooks=[], 
      excludes=excludes, 
      win_no_prefer_redirects=False, 
      win_private_assemblies=False) 

pyz = PYZ(a.pure, a.zipped_data) 

exe1 = EXE(pyz, 
      a.scripts, 
      name='slacktest', 
      exclude_binaries=True, 
      debug=False, 
      strip=False, 
      upx=True, 
      console=True) 

coll = COLLECT(exe1, 
       a.binaries, 
       a.zipfiles, 
       a.datas, 
       *tocs, 
       strip=False, 
       upx=True, 
       name='slacktest') 

回答

0

只爲那些有興趣誰的解決方案,我特此發佈由承包開發者開發的解決方案的詳細信息.....

的問題是缺少包含「cacert.pem」文件的websocket文件夾導致。 PyInstaller腳本更新如下,以便在分發目錄中複製「cacert.pem」文件。

這是PyInstaller的一個缺失功能​​。 解決這個問題的正確方法是將「hook-websocket.py」提交給PyInstaller的鉤子,以便它可以包含在PyInstaller的下一個版本中。 鉤子文件應該與hook-requests.py相似。

此外,slackclient應該引發錯誤或記錄消息,而不是僅僅在rtm_connect方法中返回False。

# -*- mode: python -*- 

import os 
import websocket 
from os.path import join, dirname, basename 
from PyInstaller.compat import modname_tkinter 

block_cipher = None 
websocket_lib_path = dirname(websocket.__file__) 
websocket_cacert_file_path = join(websocket_lib_path, 'cacert.pem') 
analysis_data = [ 
    # For websocket library to find "cacert.pem" file, it must be in websocket 
    # directory inside of distribution directory. 
    # This line can be removed once PyInstaller adds hook-websocket.py 
    (websocket_cacert_file_path, join('.', basename(websocket_lib_path))) 
] 


a = Analysis(['slacktest.py'], 
      pathex=[os.getcwd()], 
      binaries=None, 
      datas=analysis_data, 
      hiddenimports=[], 
      hookspath=[], 
      runtime_hooks=[], 
      excludes=[modname_tkinter], 
      win_no_prefer_redirects=False, 
      win_private_assemblies=False, 
      cipher=block_cipher) 
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) 
exe = EXE(pyz, 
      a.scripts, 
      exclude_binaries=True, 
      name='slacktest', 
      debug=False, 
      strip=False, 
      upx=True, 
      console=True) 
coll = COLLECT(exe, 
       a.binaries, 
       a.zipfiles, 
       a.datas, 
       strip=False, 
       upx=True, 
       name='slacktest')