SlackとVkontakteを友達にするには、PythonとAPIを使用することにしました。 以下は、VKontakteグループまたはコミュニティの壁の投稿から最新のコメントに関する情報を取得するためにSlackボットを教える方法の基本的なレシピです。
Slack API
人気のSlackメッセンジャーには、便利でシンプルなAPIがあります 。 これにより、チャットボットを簡単に管理し、チャンネルでさまざまな操作を実行できます。 チャットボットを作成する最も簡単なオプションは次のとおりです。
1. Slack.comを開きます(チームを管理するには、Slack.comに既に登録されている必要があります)。
2. slack.com/appsアプリケーション管理に移動し、「 構成 」を開きます。
3.「カスタム統合」を開き、Slack用の最初のボットを作成します。 (名前を見つけて、メッセージを送信する部屋を選択してください)
4. Slackは、ボットの認証に使用するトークンAPIを提供します。
Slack-Bot.py
初期段階は終了しました。ボットの作成を始めましょう。 動作するには、APIを操作するためのPython 3といくつかの追加ライブラリをインストールする必要があります。 slackerとVK_apiをインストールします。
$ pip install slacker $ pip install vk_api
任意のコードエディターでベースファイルSlack-Bot.pyを作成し、ボットの作成を開始します。
必要なライブラリをインポートします。
 from slacker import Slacker import vk_api
      
      
        
        
        
      
    
        
        
        
      
      
        
        
        
      
    
     
      Slackのログイントークン:
 slack = Slacker('  API token')
      
      
        
        
        
      
    
        
        
        
      
      
        
        
        
      
    
     
      VK.comにログインします。
 login, password = 'Login', 'pass' vk_session = vk_api.VkApi(login, password) try: vk_session.authorization() except vk_api.AuthorizationError as error_msg: print(error_msg) return vk = vk_session.get_api()
      
      
        
        
        
      
    
        
        
        
      
      
        
        
        
      
    
     
      そして、ボットにVkontakteグループの壁から情報を受信するように教えます。
 man_id = str(-29534144) #id ,        postidlist = vk.wall.get(owner_id=man_id, count=1, offset=0) #     a = str(postidlist['items'][0]['id']) # id       response = vk.wall.getComments(owner_id=man_id, post_id=a, count=1, sort='desc', offset=0) #       
      
      
        
        
        
      
    
        
        
        
      
      
        
        
        
      
    
     
      標準では、答えは配列の形式で提供されます。テキストを取得するには、目的の行を選択する必要があります。
 b = response['items'][0]['text'] #    
      
      
        
        
        
      
    
        
        
        
      
      
        
        
        
      
    
     
      次に、ボットで受信したテキストをSlackに送信します(#vkパラメーターは、ボットがメッセージを送信する部屋を意味します)。
 slack.chat.post_message('#vk', ' : ' + b) #     Slack
      
      
        
        
        
      
    
        
        
        
      
      
        
        
        
      
    
     
      そこで、Vkontakteグループの壁からコメントを受け取り、Slackルームに送信するための基本的な機能を取得しました。
スクリプトのロジックは次のとおりです。
1. SlackとVkontakteにログインします。
2.選択したグループに最後の投稿を依頼します。
3.投稿から最後のコメントを取得します。
4. Slackにコメントを投稿します。
特定の頻度でボットにグループウォールチェックを追加し、Slackに新しいコメントを送信することで、ボットを変更して機能を拡張できます。
動作中のボットの最終コード:
 from slacker import Slacker import vk_api import time slack = Slacker(' token Slack bot') def main(): login, password = 'login', 'pass' vk_session = vk_api.VkApi(login, password) try: vk_session.authorization() except vk_api.AuthorizationError as error_msg: print(error_msg) return vk = vk_session.get_api() while True: # man_id = str(-29534144) #id         postidlist = vk.wall.get(owner_id=man_id, count=1, offset=0) #   a = str(postidlist['items'][0]['id']) # id       time.sleep(5) ts = 10 bts = 10 while ts == bts: #    ,     ,   . response = vk.wall.getComments(owner_id=man_id, post_id=a, count=1, sort='desc', offset=0) #   ts = str(response['items'][0]['date']) #   time.sleep(5) # 5  response = vk.wall.getComments(owner_id=man_id, post_id=a, count=1, sort='desc', offset=0) bts = str(response['items'][0]['date']) print('   ' + ts + bts) time.sleep(5) #       response = vk.wall.getComments(owner_id=man_id, post_id=a, count=1, sort='desc', offset=0) #     b = response['items'][0]['text'] #     slack.chat.post_message('#vk', ' : ' + b) #     Slack    if __name__ == '__main__': main()
      
      
        
        
        
      
    
        
        
        
      
      
        
        
        
      
    
     
      完全なコードはこちらにあります 。 ご清聴ありがとうございました。