Compare commits

...

16 Commits

Author SHA1 Message Date
58d727b70b update welcome message #5201 2021-05-09 17:29:53 +02:00
b0d3227ba7 Externalized welcomebot configuraiton file (#4286). 2020-03-11 03:38:42 +01:00
89663f991b migrate init.d to systemd service for auto restart 2018-11-08 18:02:15 +01:00
5f0d1c435b auto reconnect for handling connection problems cf #3202 2018-11-06 14:57:44 +01:00
root
e5d1d76a7d remove nicks.csv from git but keep local version 2018-08-27 23:32:26 +02:00
root
bccfb520d7 add nicks.csv to gitignore 2018-08-27 23:29:04 +02:00
root
b4b89d57f4 update the welcome message #3203 2018-08-27 23:27:17 +02:00
a690a9feb3 coupage de langue persifleuse 2017-08-17 10:26:48 +02:00
b91280c9e0 Set french salutation and help keywords. 2016-10-15 13:27:42 +02:00
0477fe4358 Move the -u parameter from bot.sh to bot.py (Icinga needs) 2016-10-15 13:26:05 +02:00
fad2d01ca9 Added the nick file to the git repository. 2016-10-15 03:44:36 +02:00
137eb4732a Added service file in git repository. 2016-10-15 03:43:56 +02:00
f0ced9cb1f Enabled log. Added log filter. Set relative path detection. 2016-10-15 03:43:07 +02:00
01a5963fad Added timestamp to log. Added start message. 2016-10-15 03:41:54 +02:00
root
c9d4a64dac Creation du fichier initial avec les nicks a ignorer pour l'envoi de messages 2016-05-17 16:42:29 +02:00
root
fd09e1e4d7 Francisation et adaption a l'April 2016-05-17 16:40:48 +02:00
7 changed files with 128 additions and 29 deletions

2
.gitignore vendored
View File

@ -8,4 +8,6 @@ lib
local local
bin bin
error.log error.log
nohup.out
nicks.csv nicks.csv
bot_settings.py

21
bot_settings-example.py Normal file
View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -1 -*-
# Replace these default settings with your own personal settings
# IRC configuration
channel = "#april-accueil"
botnick = "bot_d_accueil"
server = "irc.freenode.net"
registered = False
# Bot behavior
wait_time = 60
channel_greeters = ["madix", "cioccolisa", "cpm_screen", "lonugem" ]
nick_source = "nicks.csv"
# Bot text
hello_list = ["hello", "yo", "bonjour", "salut"]
help_list = ["help", "info", "faq", "explain_yourself", "aide"]
# Bot welcome message. In fact, never used.
welcome_message = "Welcome! The channel is pretty quiet right now, so I thought I'd say hello, and ping some people that you're here. If no one responds for a whle, try emailing us at contact@april.org or just try coming back later. FYI, you're now on my list of known nicknames, so I won't bother you again."

View File

@ -1,17 +0,0 @@
# Replace these default settings with your own personal settings
# IRC configuration
channel = "#openhatch"
botnick = "welcomebot"
server = "irc.freenode.net"
registered = False
# Bot behavior
wait_time = 60
channel_greeters = ["shauna", "paulproteus", "marktraceur"]
nick_source = "nicks.csv"
# Bot text
hello_list = ["hello", "hi", "hey", "yo", "sup"]
help_list = ["help", "info", "faq", "explain_yourself"]
welcome_message = "Welcome! The channel is pretty quiet right now, so I thought I'd say hello, and ping some people that you're here. If no one responds for a whle, try emailing us at hello@openhatch.org or just try coming back later. FYI, you're now on my list of known nicknames, so I won't bother you again."

1
bot_settings.py Symbolic link
View File

@ -0,0 +1 @@
/etc/welcomebot/bot_settings.py

49
init.d.deprecated/welcomebot Executable file
View File

@ -0,0 +1,49 @@
#! /bin/sh
#
### BEGIN INIT INFO
# Provides: welcomebot
# Required-Start: $remote_fs $syslog $local_fs $network
# Required-Stop: $remote_fs $syslog $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Bot irc de l'April.
# Description: welcomebt est un bot irc, il est ici installé pour son pluging
# MeetBot qui sert à réaliser des rapports de réunion.
### END INIT INFO
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/srv/welcomebot/git/bot.sh
NAME=welcomebot
DESC=welcomebot
test -f $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --make-pidfile --pidfile /var/run/$NAME.pid \
--chuid welcomebot --exec $DAEMON -- --daemon $DAEMON \
|| return 2
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
rm -f /var/run/$NAME.pid
pkill -9 -f bot.py
echo "$NAME."
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}" >&2
exit 1
;;
esac
exit 0

39
bot.py → welcomebot.py Normal file → Executable file
View File

@ -1,7 +1,10 @@
#!/usr/bin/python -u
# -*- coding: utf-8 -1 -*-
# Welcome to WelcomeBot. Find source, documentation, etc here: https://github.com/shaunagm/WelcomeBot/ Licensed https://creativecommons.org/licenses/by-sa/2.0/ # Welcome to WelcomeBot. Find source, documentation, etc here: https://github.com/shaunagm/WelcomeBot/ Licensed https://creativecommons.org/licenses/by-sa/2.0/
# Import some necessary libraries. # Import some necessary libraries.
import socket, sys, time, csv, Queue, random, re, pdb, select, os.path import socket, sys, time, csv, Queue, random, re, pdb, select, os.path, datetime
from threading import Thread from threading import Thread
# To configure bot, please make changes in bot_settings.py # To configure bot, please make changes in bot_settings.py
@ -65,7 +68,7 @@ def irc_start(server): # pragma: no cover (this excludes this function from tes
return ircsock return ircsock
def join_irc(ircsock, botnick, channel): def join_irc(ircsock, botnick, channel):
ircsock.send("USER {0} {0} {0} :This is http://openhatch.org/'s greeter bot" ircsock.send("USER {0} {0} {0} :Robot d'accueil de https://april.org."
".\n".format(botnick)) # bot authentication ".\n".format(botnick)) # bot authentication
ircsock.send("NICK {}\n".format(botnick)) # Assign the nick to the bot. ircsock.send("NICK {}\n".format(botnick)) # Assign the nick to the bot.
if os.path.isfile("password.txt"): if os.path.isfile("password.txt"):
@ -80,7 +83,8 @@ def join_irc(ircsock, botnick, channel):
def msg_handler(ircsock): # pragma: no cover (this excludes this function from testing) def msg_handler(ircsock): # pragma: no cover (this excludes this function from testing)
new_msg = ircsock.recv(2048) # receive data from the server new_msg = ircsock.recv(2048) # receive data from the server
new_msg = new_msg.strip('\n\r') # removing any unnecessary linebreaks new_msg = new_msg.strip('\n\r') # removing any unnecessary linebreaks
print(new_msg) #### Potentially make this a log instead? if new_msg != '':
print(datetime.datetime.now().isoformat() + " " + new_msg) #### Potentially make this a log instead?
return new_msg return new_msg
# Called by bot on startup. Builds a regex that matches one of the options + (space) botnick. # Called by bot on startup. Builds a regex that matches one of the options + (space) botnick.
@ -100,14 +104,21 @@ def get_regex(options, botnick):
# Welcomes the "person" passed to it. # Welcomes the "person" passed to it.
def welcome_nick(newcomer, ircsock, channel, channel_greeters): def welcome_nick(newcomer, ircsock, channel, channel_greeters):
ircsock.send("PRIVMSG {0} :Welcome {1}! The channel is pretty quiet " ircsock.send("PRIVMSG {0} :Bonjour {1} Je suis un robot IRC. Ce salon est tranquille actuellement, "
"right now, so I thought I'd say hello, and ping some people " "je me permets donc de vous dire bonjour. Je signale à des actifs de l'April "
"(like {2}) that you're here. If no one responds for a " "(comme {2}) que vous êtes là."
"while, try emailing us at hello@openhatch.org or just try "
"coming back later. FYI, you're now on my list of known "
"nicknames, so I won't bother you again."
"\n".format(channel, newcomer, greeter_string(channel_greeters))) "\n".format(channel, newcomer, greeter_string(channel_greeters)))
ircsock.send("PRIVMSG {0} :{1} Si personne ne répond d'ici quelques minutes, "
"vous pouvez nous envoyer un courriel à contact@april.org (vous recevrez une demande "
"de confirmation, merci d'y répondre pour diffuser votre message) ou revenir plus tard."
"\n".format(channel, newcomer))
ircsock.send("PRIVMSG {0} :{1} Vous êtes désormais dans ma liste d'identifiants connus. "
"Je ne vous enverrai plus de message."
"\n".format(channel, newcomer))
# Checks and manages the status of newcomers. # Checks and manages the status of newcomers.
def process_newcomers(bot, newcomerlist, ircsock, channel, greeters, welcome=1): def process_newcomers(bot, newcomerlist, ircsock, channel, greeters, welcome=1):
for person in newcomerlist: for person in newcomerlist:
@ -127,7 +138,7 @@ def parse_messages(ircmsg):
# Cleans a nickname of decorators/identifiers # Cleans a nickname of decorators/identifiers
def clean_nick(actor): def clean_nick(actor):
if actor: # In case an empty string gets passed if actor: # In case an empty string gets passed
if actor.find("openhatch") != -1: # If nick is like "openhatch_1234" don't clean. if actor.find("april") != -1: # If nick is like "openhatch_1234" don't clean.
return actor return actor
actor = actor.replace("_", "") # Strip out trailing _ characters actor = actor.replace("_", "") # Strip out trailing _ characters
while(actor[-1]) in "1234567890": # Remove trailing numbers while(actor[-1]) in "1234567890": # Remove trailing numbers
@ -198,9 +209,9 @@ def greeter_string(greeters):
if len(greeters) > 2: if len(greeters) > 2:
for name in greeters[:-1]: for name in greeters[:-1]:
greeterstring += "{}, ".format(name) greeterstring += "{}, ".format(name)
greeterstring += "and {}".format(greeters[-1]) greeterstring += "et {}".format(greeters[-1])
elif len(greeters) == 2: elif len(greeters) == 2:
greeterstring = "{0} and {1}".format(greeters[0], greeters[1]) greeterstring = "{0} et {1}".format(greeters[0], greeters[1])
else: else:
greeterstring = greeters[0] greeterstring = greeters[0]
return greeterstring return greeterstring
@ -232,6 +243,7 @@ def pong(ircsock, ircmsg):
########################## ##########################
def main(): def main():
print datetime.datetime.now().isoformat() + " WelcomeBot starting…"
ircsock = irc_start(settings.server) ircsock = irc_start(settings.server)
join_irc(ircsock, settings.botnick, settings.channel) join_irc(ircsock, settings.botnick, settings.channel)
WelcomeBot = Bot() WelcomeBot = Bot()
@ -239,10 +251,13 @@ def main():
ready_to_read, b, c = select.select([ircsock],[],[], 1) # b&c are ignored here ready_to_read, b, c = select.select([ircsock],[],[], 1) # b&c are ignored here
process_newcomers(WelcomeBot, [i for i in WelcomeBot.newcomers if i.around_for() > WelcomeBot.wait_time], ircsock,settings.channel, settings.channel_greeters) process_newcomers(WelcomeBot, [i for i in WelcomeBot.newcomers if i.around_for() > WelcomeBot.wait_time], ircsock,settings.channel, settings.channel_greeters)
if ready_to_read: if ready_to_read:
last_read = datetime.datetime.utcnow()
ircmsg = msg_handler(ircsock) # gets message from ircsock ircmsg = msg_handler(ircsock) # gets message from ircsock
ircmsg, actor = parse_messages(ircmsg) # parses it or returns None ircmsg, actor = parse_messages(ircmsg) # parses it or returns None
if ircmsg is not None: # If we were able to parse it if ircmsg is not None: # If we were able to parse it
message_response(WelcomeBot, ircmsg, actor, ircsock, settings.channel, settings.channel_greeters) # Respond to the parsed message message_response(WelcomeBot, ircmsg, actor, ircsock, settings.channel, settings.channel_greeters) # Respond to the parsed message
if datetime.datetime.utcnow() - last_read > datetime.timedelta(minutes=10):
raise Exception('timeout: nothing to read on socket since 10 minutes')
if __name__ == "__main__": # This line tells the interpreter to only execute main() if the program is being run, not imported. if __name__ == "__main__": # This line tells the interpreter to only execute main() if the program is being run, not imported.
sys.exit(main()) sys.exit(main())

15
welcomebot.service Normal file
View File

@ -0,0 +1,15 @@
[Unit]
Description=APRIL IRC bot for april-accueil channel
After=network-online.target
[Service]
Type=simple
WorkingDirectory=/srv/welcomebot/git
ExecStart=/srv/welcomebot/bin/welcomebot.py
User=welcomebot
Group=welcomebot
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target

13
welcomebot.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/sh
BINDIR="`dirname "$0"`"
HOMEDIR="`dirname "$BINDIR"`"
echo "BINDIR=$BINDIR"
echo "HOMEDIR=$HOMEDIR"
cd "$BINDIR"
#nohup /srv/welcomebot/git/bot.py >> /dev/null 2>>../welcomebot.log &
nohup "$BINDIR/welcomebot.py" > /dev/null 2>>"$HOMEDIR/welcomebot.log" &