The story of creating a VIP channel in in-house slack
I made a chatwork version because the above entry was interesting.
--Prepare an anonymous channel and a bot account to speak. --Use Chatwork Webhook with your bot account. --Receive the webhook on AWS API Gateway and fire Lambda. --From Lambda, use Chatwork API to speak to anonymous channels with your bot account.
Let's do it
Create an anonymous channel and a bot account to speak. Add all the users you want to play with to the anonymous channel. After creating a bot account, go to Account Name> API Settings at the top right of the ChatWork screen. Set the API so that it can be used with the created bot account.
When you can use the API, first create an API Token. (I will use it later) Also, create a new webhook. The URL can be changed later, so it's okay here. OK if the event is an account event. Please give it a name.
I tried to verify the request sent from ChatWork Webhook with Lambda Read this first. read. You can get a feel for it.
Create a new function in AWS Lambda. Since Python will be used this time, please set the runtime to Python 3.7 etc.
Once created, from ** Add Trigger ** Register the API Gateway. Set it like this. Ignore all security settings for quick operation, but set them as needed.
When you return to the lambda settings screen As you can see at the bottom of the image, you can see the API endpoint a little below. Let's set this to the chatwork webhook URL.
By the way, as the brain did not want to send POST from other than requests, as a preparation on the source code side [Verification] I immediately tried Lambda's Layer function #reinvent Add a layer with requests module referring to the above entry. Of course, there is also a way to remember how to use urllib. Please choose the one who feels the cost is cheap.
import json
import os
import requests
def lambda_handler(event, context):
url = f'https://api.chatwork.com/v2/rooms/{os.environ['ROOM_ID']}/messages'
headers = {'X-ChatWorkToken': os.environ['CW_KEY']}
content = json.loads(event['body']) #The body of event seems to come as a string
content = content['webhook_event']['body'] #If you made it an account event, here is the message body
content = content.replace('[To:bot_account_id]bot account name\n', '') #Removed incoming wording when spoken in To
params = {'body': f'{content}'}
res = requests.post(url, data=params, headers=headers)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
It is the minimum movement Taro. Write this in the editor part of Lambda. Actually, it is better to insert authentication using TOKEN that comes on the Webhook, and change the content to return according to the result of processing. The person who wrote this wanted to move it anyway, so I broke it. I'm sorry
I think there is an input field for environment variables just below the editor part, so set the ID of the anonymous channel and the API Token of the anonymous account. The ID of the channel uses the number in the URL when the channel is opened in the browser.
will move. It was good.
I made it to take a break from overtime work, so I'm not confident in the content. If you want to throw Masakari, please throw more and more. Please point out if there is something wrong with it. I will erase it.
Thank you very much.
Recommended Posts