[PYTHON] The story of creating a VIP channel for in-house chatwork

Let's go

The story of creating a VIP channel in in-house slack

I made a chatwork version because the above entry was interesting.

Thing you want to do

--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

Do chatwork

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. image.png 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. image.png The URL can be changed later, so it's okay here. OK if the event is an account event. Please give it a name.

Do AWS

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. image.png

Once created, from ** Add Trigger ** image.png Register the API Gateway. image.png Set it like this. Ignore all security settings for quick operation, but set them as needed.

When you return to the lambda settings screen image.png 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.

Creating a function

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.

Try to move

will move. It was good.

in conclusion

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

The story of creating a VIP channel for in-house chatwork
The story of creating a database using the Google Analytics API
The story of writing a program
The story of creating a "spirit and time chat room" exclusively for engineers in the company
The story of making a standard driver for db with python.
The story of creating a site that lists the release dates of books
The story of blackjack A processing (python)
The story of creating a bot that displays active members in a specific channel of slack with python
The story of low learning costs for Python
The story of creating a store search BOT (AI LINE BOT) for Go To EAT in Chiba Prefecture (1)
Image processing? The story of starting Python for
The story of making a lie news generator
A story of creating 16 * 16 dots from a Digimon photo
The story of making a mel icon generator
The story of Django creating a library that might be a little more useful
The story of creating a store search BOT (AI LINE BOT) for Go To EAT in Chiba Prefecture (2) [Overview]
The story of sys.path.append ()
The story of launching a Minecraft server from Discord
A story that reduces the effort of operation / maintenance
The story of making a music generation neural network
A story about changing the master name of BlueZ
Zip 4 Gbyte problem is a story of the past
A story that analyzed the delivery of Nico Nama.
A story about improving the program for partial filling of 3D binarized image data
The story of switching from WoSign to Let's Encrypt for a free SSL certificate
Now in Singapore The story of creating a LineBot and wanting to do a memorable job
Avoiding the pitfalls of using a Mac (for Linux users?)
The story of building Zabbix 4.4
[Apache] The story of prefork
The story of a Django model field disappearing from a class
The story of making a question box bot with discord.py
The story of releasing a Python text check tool on GitHub x CircleCI for the first time
A story that is a little addicted to the authority of the directory specified by expdp (for beginners)
A memorandum of understanding for the Python package management tool ez_setup
A story stuck with the installation of the machine learning library JAX
A story that struggled to handle the Python package of PocketSphinx
The story of making a module that skips mail with python
The story of Python and the story of NaN
Step by Step for creating a Dockerfile
The story of participating in AtCoder
The story of the "hole" in the file
The story of remounting the application server
[AtCoder for beginners] A story about the amount of calculation that you want to know very roughly
A story about trying to improve the testing process of a system written in C language for 20 years
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
A story that visualizes the present of Qiita with Qiita API + Elasticsearch + Kibana
The story of a Parking Sensor in 10 minutes with GrovePi + Starter Kit
The story of making a university 100 yen breakfast LINE bot with Python
Randomly play the movie on ChromeCast for a certain period of time
A memo for utilizing the unit test mechanism KUnit of the Linux kernel
Latin learning for the purpose of writing a Latin sentence analysis program (Part 1)
The story of having a hard time introducing OpenCV with M1 MAC
A story about creating an anonymous channel on Slack from zero knowledge
The story of developing a web application that automatically generates catchphrases [MeCab]
A story of a person who started aiming for data scientist from a beginner
The story of making a sound camera with Touch Designer and ReSpeaker
The story of the escape probability of a random walk on an integer grid
The story of making a package that speeds up the operation of Juman (Juman ++) & KNP
What Java users thought of using the Go language for a day
The story of creating Botonyan that returns the contents of Google Docs in response to a specific keyword on Slack
In creating a model for discriminating tweet emotions with LSTM + Embedding, I reaffirmed the importance of preprocessing in NLP.