4. Kei IWASAKI
Web Application/Infrastructure Engineer
working at SQUEEZE Inc in Japan.
From Tokyo, Japan.
Twitter: @laugh_k, Github: @laughk
Co-authors of "スラスラわかるPython"
4 / 63
6. My other activities related to Python
in Japan
Python mini hack-a-thon
manthly event in Tokyo
Lecturer of Python ⼊⾨者の集い(hands-on events for Python begginer)
PyCon JP
PyCon JP 2015: LT
PyCon JP 2016: talk speaker
PyCon JP 2017: panel discussion
6 / 63
31. Use the slackbot function below
listen_to
called when a message matching the pattern is sent on a channel/private
channel chat.
response_to
called when a message matching the pattern is sent to the bot.
31 / 63
32. listen_to
called when a message matching the pattern is sent on a channel/private channel
chat.
@listen_to(r'HAHAHA')
def what_is_funny(message):
message.send('What is funny?')
32 / 63
33. response_to
called when a message matching the pattern is sent to the bot.
@respond_to(r'parrots+(.+)')
def parrot(message, word):
message.reply(word)
33 / 63
37. simple memo plugin with PeeWee
make data modeles, as models.py like below.
import os
from peewee import SqliteDatabase, Model, CharField, TextField
db = SqliteDatabase(os.path.join(os.path.dirname(__file__), 'bot_database.db'))
class Memo(Model):
name = CharField(primary_key=True)
text = TextField()
class Meta:
database = db
db.connect()
db.create_tables([Memo], safe=True)
37 / 63
38. simple memo plugin with PeeWee
add functions to plugin.py like below.
from models import Memo
...
@respond_to(r'memos+saves+(S+)s+(S.*)')
def memo_save(message, name, text):
memo, created = Memo.get_or_create(name=name, text=text)
memo.save()
message.reply(f'I remembered a memo "{name}"')
@respond_to(r'memos+shows+(S+)')
def memo_show(message, name):
memo = Memo.get_or_none(name=name)
if memo:
message.reply(f'memo "{name}" is belown```n{memo.text}n```n')
else:
message.reply(f'memo "{name}" ... hmm ..., I don't know ¯_(ツ)_/¯')
38 / 63
49. make simple weather information plugin
add parameter information to slackbot_setting.py like below
API_TOKEN = '<set a bot user token from your slack team>'
DEFAULT_REPLY = 'Hi :raising_hand: I'm Slackbot'
PLUGINS = [
'plugin'
]
OPENWEATHERMAP_APPID = 'b6******************************' # put Your APPID
you can use a parameter
>>> from slackbot import settings
>>> settings.OPENWEATHERMAP_APPID
b6******************************
49 / 63
50. make simple weather information plugin
add functions to plugin.py like below.
import json, requests
from slackbot import settings
...
@respond_to(r'weathers+(S+)')
def show_weather(message, city):
url = 'http://api.openweathermap.org/data/2.5/weather'
result = requests.get(
f'{url}?q={city}&appid={settings.OPENWEATHERMAP_APPID}'
)
data_dict = json.loads(result.content.decode())
if result.status_code == 200:
message.reply('nCurrent Weathern'
f'{data_dict["name"]}: {data_dict["weather"][0]["description"]}')
else:
message.reply('Sorry, I could not get weather Information :sob:')
50 / 63
58. slackbot x Slacker x PeeWee
Slacker
https://github.com/os/slacker
Full-featured Python interface for the Slack API
you can use slack function more than using slackbot (ex. getting slack userid)
58 / 63
59. Plusplus Plugin
count of Karma
you can Praise the team members!
this source code from pyconjpbot
https://github.com/pyconjp/pyconjpbot/blob/master/pyconjpbot/plugins/pluspl
59 / 63
60. You can let a slackbot do anything
else you can do with Python
60 / 63