投稿時間:2023-07-29 10:07:32 RSSフィード2023-07-29 10:00 分まとめ(8件)
カテゴリー等 | サイト名等 | 記事タイトル・トレンドワード等 | リンクURL | 頻出ワード・要約等/検索ボリューム | 登録日 |
---|---|---|---|---|---|
AWS | AWSタグが付けられた新着投稿 - Qiita | 非インフラエンジニアがAWS SAA-C03に合格した方法 | https://qiita.com/renwhite0802/items/6c3cc9d5181452f849d7 | awssaac | 2023-07-29 09:57:41 |
Azure | Azureタグが付けられた新着投稿 - Qiita | AzureSQLDBのDTUを上げたら負荷テスト結果は向上するか | https://qiita.com/kimura_r76/items/f01eb96c2e1546e4988b | azuresql | 2023-07-29 09:37:42 |
海外TECH | DEV Community | Building a Slack Bot in Python | https://dev.to/haszankauna/building-a-slack-bot-in-python-146d | Building a Slack Bot in Python IntroductionIn this article we will be building a bot in python using the Slack API We ll go through setting up the development environment and also getting the slack API bot token Tools needed This bot as mentioned earlier will be requiring python and the Slack API but in order to make use of this we will need certain tools which we will be making use of as follows Download Pythonpip and virtualenvA Slack account Setting Up The EnvironmentWhile we are now aware of the tools and software needed to be able to get coding we will also need to set up our development environment by going to the command line and changing the directory of where to keep the project folder We will then need to create a virtual environment to set apart our application dependencies from other projects by inputing the following code to our command line virtualenv slackbotWe will then need to activate the virtual environment by inputing the code below source slackbot bin activateIn addition the slackclient API helper library by slack can then send and receive messages from a slack channel By using the following code below we will then need to install the slackclient library using the pip command below pip install slackclientThe next step in the process is to create a Slack App to receive our API token for the bot You will then need to pick an app name as shown below The next step is choosing a workspace if you re already signed into one or create a new workspace as shown APIs and ConfigurationThe whole idea of the slack bot is ensuring that it operates like any other user participating in conversations in DMs channels and groups This is called a bot user that can be set up by selecting bot users under the features section Below are a couple of steps involved in adding a bot user The first step is clicking on the already created workspaceThe next step is going to the App home in the navigation section and then clicking on the review scopes to add button After clicking on app home the next thing is to go to the bots page to get a token and get started with configuring the API Following the steps above will lead to a page shown below As shown above the next step is generating a token and in the button shown in the bottom right corner of the screen A usual practice is to export secret tokens as environment variables as shown below with the name SLACK TOKEN export SLACK TOKEN your bot user access token here We have now authorised the Slack RTM and Web API Bot CodingIn your new folder create a new file named bot py and input the code below import osimport timeimport refrom slackclient import SlackClientOur dependencies have now been imported and we can now get the values associated with the environment variable and then represent the Slack client as shown in the code below represent the Slack clientslack client SlackClient os environ get SLACK TOKEN bot s user ID in Slack value is assigned after the bot starts upbot id None constantsRTM READ DELAY second delay between reading from RTMEXAMPLE COMMAND do MENTION REGEX lt WU gt The code above now represents the SlackClient with the SLACK TOKEN which is then exported as an environment variable This now communicates that we could use a variable to store our Slack user ID of the bot We can then explain the constants which have been declared in the code below if name main if slack client rtm connect with team state False print Bot connected and running Read bot s user ID by calling Web API method auth test starterbot id slack client api call auth test user id while True command channel parse bot commands slack client rtm read if command handle command command channel time sleep RTM READ DELAY else print Connection failed Exception traceback printed above Our Slack client now connects to the Slack RTM API which now calls our Web API method auth test to get the bot s ID The bot user now has a user ID for each workspace the Slack App is installed within Another point to note is that the program now goes to an infinite loop where each time the loop runs the client it receives any event that arrives from Slack s RTM API With careful notice we will see that before the loop ends the program pauses for a second such as it doesn t loop so fast and not waste our CPU time For each event been read the parse bot comands function now ensures the event contains a command for the bot This will also ensure the command will contain a value while ensuring the handle command function knows what to do with the command We now have the above explained in the code below def parse bot commands slack events Parses a list of events coming from the Slack RTM API to find bot commands If a bot command is found this function returns a tuple of command and channel If its not found then this function returns None None for event in slack events if event type message and not subtype in event user id message parse direct mention event text if user id bot id return message event channel return None Nonedef parse direct mention message text Finds a direct mention a mention that is at the beginning in message text and returns the user ID which was mentioned If there is no direct mention returns None matches re search MENTION REGEX message text the first group contains the username the second group contains the remaining message return matches group matches group strip if matches else None None def handle command command channel Executes bot command if the command is known Default response is help text for the user default response Not sure what you mean Try format EXAMPLE COMMAND Finds and executes the given command filling in response response None This is where you start to implement more commands if command startswith EXAMPLE COMMAND response Sure write some more code then I can do that Sends the response back to the channel slack client api call chat postMessage channel channel text response or default response The parse bot command function now takes these events and ensure that they are pointed to the Bot While there are several event types which our bot will come across we need to find the commands we want using the message events Message events generally have sub types but in the commands we will find sub types won t be defined These function now filters the not so great events by checking the properties The next step being taken into consideration is wanting to know if the event represents a message but also finding out if the Bot is mentioned Our parse direct mention function will then take the task upon itself of figuring out if the text starts with a mention and then compare it to the user ID we stored in the Bot which if being the same we know is a bot command and then returns the command text in the channel ID The parse direct mentions function will take an expression to determine if a user being confirm if a user has been mentioned in the message Using this it then gives us back the user ID with the message none if there is no mention Our last function handle command is where for future reference adds what makes up the slack bot It carries a known command to make use of by sending a response to Slack by using the chat postMessage to call back our Web API The code below shows an explanationimport osimport timeimport refrom slackclient import SlackClient instantiate Slack clientslack client SlackClient os environ get SLACK BOT TOKEN slack user ID in Slack value is assigned after the bot starts upslack id None constantsRTM READ DELAY second delay between reading from RTMEXAMPLE COMMAND do MENTION REGEX lt WU gt def parse bot commands slack events Parses a list of events coming from the Slack RTM API to find bot commands If a bot command is found this function returns a tuple of command and channel If its not found then this function returns None None for event in slack events if event type message and not subtype in event user id message parse direct mention event text if user id starterbot id return message event channel return None Nonedef parse direct mention message text Finds a direct mention a mention that is at the beginning in message text and returns the user ID which was mentioned If there is no direct mention returns None matches re search MENTION REGEX message text the first group contains the username the second group contains the remaining message return matches group matches group strip if matches else None None def handle command command channel Executes bot command if the command is known Default response is help text for the user default response Not sure what you mean Try format EXAMPLE COMMAND Finds and executes the given command filling in response response None This is where you start to implement more commands if command startswith EXAMPLE COMMAND response Sure write some more code then I can do that Sends the response back to the channel slack client api call chat postMessage channel channel text response or default response if name main if slack client rtm connect with team state False print Starter Bot connected and running Read bot s user ID by calling Web API method auth test starterbot id slack client api call auth test user id while True command channel parse bot commands slack client rtm read if command handle command command channel time sleep RTM READ DELAY else print Connection failed Exception traceback printed above We can now run the code by using the following code below python starterbot pyThe next step is creating a channel as shown below You can now start giving commands to your bot ConclusionIn conclusion we looked at creating a Slack bot in this article and proper implementation and usage of the Slack API A common application of this bot could be in the creation of an onboarding bot to your Slack application It is also important to integrate a database and also play around with other APIs to integrate to Slack | 2023-07-29 00:57:28 |
ニュース | BBC News - Home | Australia helicopter crash: Four military aircrew missing | https://www.bbc.co.uk/news/world-australia-66344550?at_medium=RSS&at_campaign=KARANGA | lindeman | 2023-07-29 00:28:33 |
ビジネス | 東洋経済オンライン | 【アルコール依存症】休日「昼飲み」がリスクの訳 心当たりがある人は、チェックリストで確認を | 「病気」と「症状」の対処法 | 東洋経済オンライン | https://toyokeizai.net/articles/-/689770?utm_source=rss&utm_medium=http&utm_campaign=link_back | 心当たり | 2023-07-29 09:30:00 |
ビジネス | プレジデントオンライン | 選手、審判、応援団、観客がバタバタと倒れている…今年の「夏の甲子園予選」で起きている異様な光景 - なぜサッカー協会のような対策ができないのか | https://president.jp/articles/-/72240 | 体調不良 | 2023-07-29 10:00:00 |
ビジネス | プレジデントオンライン | 都心マンション超高騰のカギを握る高所得女性たちが、価格以外に抱えている住まいへの「ある不満」とは - 購入価格から間取り、機能までキャリア女性約400人の「住まい」を徹底調査 | https://president.jp/articles/-/72055 | 高騰 | 2023-07-29 10:00:00 |
ビジネス | プレジデントオンライン | グリーンランドを独立させて親中国家に…中国が密かに進める「氷上シルクロード」構想の恐ろしさ - 現地の自治政府は中国からの投資を歓迎 | https://president.jp/articles/-/71516 | 国家主席 | 2023-07-29 10:00:00 |
コメント
コメントを投稿