[PYTHON] Send a direct love email to a new user when a new user joins the Slack team

Introduction

I belong to an organization called kosen11s that has only a metamorphosis with a name like a certain super-dimensional soccer anime team. At first, there was a lot of noise saying "_Automation! Automation! Automation !!!!", so I automated it.

main.jpg

(I'm sorry for Level Five !!!)

Source code

kosen11s/slack_welcome_kosen11s

Take a look at the GitHub repository! !! I want to make it feel like

environment

Python 3.5.1 pip 8.1.2

Implementation

$ git clone https://github.com/kosen11s/slack_welcome_kosen11s.git
$ cd slack_welcome_kosen11s
$ pip install -r requiredments.txt

I will omit the API key etc. of Slack this time! !! I'm sorry!

run.py


from slackbot.bot import Bot
import slackbot_settings

if __name__ == "__main__":
	bot = Bot()
	bot.run()

I don't know if it makes sense to keep this running all the time, but isn't it nice to have a logged-in user?

welcome_bot.py


from slacker import Slacker
import json, requests
import slackbot_settings

welcomeMessage = """
Welcome to kosen11s:tada::tada::tada::tada:\n
First of all#Introduce yourself briefly on the profile channel:heart:\n
Other#github , #muscle , #nsfw , #There is skype! Please enter properly:raryosu:\n
And this is the last! Please fill in the skill sheet linked below!\n
The URL is Hi, Mi, Tsu ♡\n
Nice to meet you:octocat:
"""

class Member(object):
	memberCount = 0
	def setMember(self):
		data = getJson()
		self.memberCount = len(data['channel']['members'])

def getJson():
	url = 'https://slack.com/api/channels.join'
	parameters = {
		'token' : slackbot_settings.API_TOKEN_TEAM,
		'name' : 'random'}
	r = requests.get(url, params = parameters)
	data = json.loads(r.text)
	mem = Member()
	if mem.memberCount == 0:
		mem.memberCount = len(data['channel']['members'])
	return data

def welcomePost(user):
	slack = Slacker(slackbot_settings.API_TOKEN)
	slack.chat.post_message(
		'@' + user,
		'Hi! ' + user + " " + welcomeMessage,
		as_user = True,
		link_names = 1
	)

if __name__ == '__main__':
	mem = Member()
	mem.setMember()
	while True:
		jsonChannel = getJson()
		if mem.memberCount < len(jsonChannel['channel']['members']):
			print(mem.memberCount)
			mem.memberCount = len(jsonChannel['channel']['members'])
			url = 'https://slack.com/api/users.info'
			parameters = {
				'token' : slackbot_settings.API_TOKEN_TEAM,
				'user' : jsonChannel['channel']['members'][0]}
			r_2 = requests.get(url, params = parameters)
			jsonUser = json.loads(r_2.text)
			welcomePost(jsonUser['user']['name'])

THE is suitable.

slackbot_settings.py


API_TOKEN = 'xoxb-***********-************************'
API_TOKEN_TEAM = 'xoxp-***********-***********-***********-**********'

What if you run it with this? ?? ??

スクリーンショット_2016-08-02_午後8_40_51.png

A message came to me (directly specifying the user for testing) who was transformed into a new person like this! !! !!

At the end

For some reason, I stuck in one leg before the chatbot got hot, but why don't you try making a SlackBOT or something easily?

スクリーンショット 2016-08-02 午後8.51.12.png

Recommended Posts

Send a direct love email to a new user when a new user joins the Slack team
Send a message from Python to Slack
How to mention a user group in slack notification, how to check the id of the user group
[Python] Throw a message to the slack channel
Send a push message to the LINE Bot when the LTE-M Button is pressed [SORACOM]
I want to send a business start email automatically
ABC's A problem analysis for the past 15 times to send to those who are new to Python
How to create a new file when the specified file does not exist — write if the file exists
Code to send an email based on the Excel email list
Change the installation destination when --user is added to pip
Write a script in Shell and Python to notify you in Slack when the process is finished