As the title says,
root@4c506a68cd26:~# python --version
Python 3.7.7
root@4c506a68cd26:~# mecab --version
mecab of 0.996
I will omit it because it only follows the "referenced article".
I didn't understand one point, or misunderstood, I thought that there was a setting to specify the endpoint somewhere when deploying locally or to heroku, so I searched variously.
I didn't write it anywhere, but somehow I came to a conclusion, so I wrote it so that I don't forget
Whether it's local or running on a server on the Internet, it connects to the Discord server and is recognized as online while client.run (TOKEN)
is running and running. I think that's what it means.
I haven't read the source code and it's not reliable information, but I think that's probably the case.
Log in to heroku from the CLI.
heroku login
Log in to heroku in your browser.
Log in to the Container Registry on Heroku.
heroku container:login
Set Discord TOKEN.
heroku config:set TOKEN=hogehoge
FROM python:3.7-slim
ENV HOME=/app
WORKDIR /app
#Used to install NEologd
RUN apt-get update && apt-get install -y \
build-essential \
curl \
git \
openssl \
sudo \
zip \
file
#Install MeCab
RUN apt-get update && apt-get install -y \
mecab \
libmecab-dev \
mecab-ipadic \
mecab-ipadic-utf8
RUN cd /usr/share/mecab && \
git clone https://github.com/neologd/mecab-ipadic-neologd.git && \
cd mecab-ipadic-neologd/ && \
./bin/install-mecab-ipadic-neologd -n -a -y -p /usr/share/mecab/dic/mecab-ipadic-neologd/
COPY requirements.txt ./requirements.txt
RUN pip install -r requirements.txt
COPY . .
# discord_bot.Run py to launch the bot
CMD ["python", "discord_bot.py"]
requirements.txt
mecab-python3
discord.py
From all messages, without using commands
discord_bot.py
import os
import MeCab
import discord
TOKEN = os.environ["TOKEN"]
PREFIX = "mecab "
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.event
async def on_message(message):
print("received message: " + str(message))
if message.content.startswith(PREFIX):
#Does not respond if the sender is a bot
if client.user != message.author:
print(message.content)
splited_message = message.content.split() #Divide by space
splited_message.pop(0) #The beginning is"mecab"So unnecessary
content = splited_message.pop() #Treat the end as a character string to be parsed
option = ' '.join(splited_message) # "mecab "From"{Target character string}"Concatenate strings between
mecab = MeCab.Tagger(option)
m = "```" + mecab.parse(content) + "```"
print(m)
#Send a message to the channel to which the message was sent
await message.channel.send(m)
client.run(TOKEN)
heroku container:push web --app discord-bot-sample-app
heroku container:release web --app discord-bot-sample-app
If the deployment is successful, check the ** SCOPES ** on the OAuth2 page of the Discord developer page to access the URL displayed at the bottom and connect the bot to the server.
This should make the bot work (should).
mecab The operating environment is reflected in this public repository. When you message
This noun,Pronoun,General,*,*,*,Here,Click here,Click here
Particles,Attributive,*,*,*,*,of,No,No
Public noun,Change connection,*,*,*,*,Release,Kokai,Kokai
Repository noun,General,*,*,*,*,*
Particles,Case particles,General,*,*,*,To,D,D
Action noun,Change connection,*,*,*,*,motion,Dousa,Dosa
Environmental noun,General,*,*,*,*,environment,Kankyo,Kankyo
Particles,Case particles,General,*,*,*,To,Wo,Wo
Reflection noun,Change connection,*,*,*,*,Reflect,Hanei,Hanei
Verb,Independence,*,*,Sahen Suru,Continuous form,To do,Shi,Shi
Particles,Connection particle,*,*,*,*,hand,Te,Te
Yes verb,Non-independent,*,*,Five steps, La line,Continuous form,is there,Ants,Ants
Auxiliary verb,*,*,*,Special / mass,Uninflected word,Masu,trout,trout
.. symbol,Kuten,*,*,*,*,。,。,。
EOS
I get a reply like this.
mecab -d / usr / share / mecab / dic / mecab-ipadic-neologd / First, let's prepare the target character string. If you specify a NEologd dictionary like
,
This noun,Pronoun,General,*,*,*,Here,Click here,Click here
Particles,Attributive,*,*,*,*,of,No,No
Public noun,Change connection,*,*,*,*,Release,Kokai,Kokai
Repository noun,Proper noun,General,*,*,*,Repository,Repository,Repository
Particles,Case particles,General,*,*,*,To,D,D
Operating environment noun,Proper noun,General,*,*,*,Operating environment,Dousakankyo,Dosa Kankyo
Particles,Case particles,General,*,*,*,To,Wo,Wo
Reflection noun,Change connection,*,*,*,*,Reflect,Hanei,Hanei
Verb,Independence,*,*,Sahen Suru,Continuous form,To do,Shi,Shi
Particles,Connection particle,*,*,*,*,hand,Te,Te
Yes verb,Non-independent,*,*,Five steps, La line,Continuous form,is there,Ants,Ants
Auxiliary verb,*,*,*,Special / mass,Uninflected word,Masu,trout,trout
.. symbol,Kuten,*,*,*,*,。,。,。
EOS
Only the operating environment
has changed, but it seems that the NEologd dictionary is used properly.
With this implementation, it will receive all the messages of the server to which the bot is connected and check if the mecab
string exists (I think, maybe).
That is wasteful because it costs money to process most irrelevant messages.
If you implement the command, you will only be able to receive the message if the prefix and command strings are present (should, maybe).
discord_command_bot.py
import os
import MeCab
import discord
from discord.ext import commands
TOKEN = os.environ["TOKEN"]
bot = commands.Bot(command_prefix='!', description='Output to results of morphological analysis.')
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
@bot.command()
async def mecab(ctx, *args):
print("received message: " + str(args))
if bot.user != ctx.message.author:
l = list(args)
content = l.pop() #Treat the end as a character string to be parsed
print(content)
option = ' '.join(l)
mecab = MeCab.Tagger(option)
m = "```" + mecab.parse(content) + "```"
print(m)
#Send a message to the channel to which the message was sent
await ctx.send(m)
bot.run(TOKEN)
The difference from the non-command version is
client (discord.Client)
is now bot (commands.Bot)
.mecab
method.Rewrite the Dockerfile to execute with the command version.
CMD ["python", "discord_commmand_bot.py"]
Deploy to heroku.
heroku container:push web --app discord-bot-sample-app
heroku container:release web --app discord-bot-sample-app
The ! Mecab
command should now be valid.
`! mecab The operating environment is reflected in this public repository. ``
This noun,Pronoun,General,*,*,*,Here,Click here,Click here
Particles,Attributive,*,*,*,*,of,No,No
Public noun,Change connection,*,*,*,*,Release,Kokai,Kokai
Repository noun,General,*,*,*,*,*
Particles,Case particles,General,*,*,*,To,D,D
Action noun,Change connection,*,*,*,*,motion,Dousa,Dosa
Environmental noun,General,*,*,*,*,environment,Kankyo,Kankyo
Particles,Case particles,General,*,*,*,To,Wo,Wo
Reflection noun,Change connection,*,*,*,*,Reflect,Hanei,Hanei
Verb,Independence,*,*,Sahen Suru,Continuous form,To do,Shi,Shi
Particles,Connection particle,*,*,*,*,hand,Te,Te
Yes verb,Non-independent,*,*,Five steps, La line,Continuous form,is there,Ants,Ants
Auxiliary verb,*,*,*,Special / mass,Uninflected word,Masu,trout,trout
.. symbol,Kuten,*,*,*,*,。,。,。
EOS
`mecab The operating environment is reflected in this public repository. ``
No reaction.
success.
It works as a bot for a few minutes after deploying, but soon goes offline. Also, if you deploy or turn dyno on / off, it will return, but I want to keep it running all the time.
https://teratail.com/questions/242975
When I looked it up, it seems that it was not good to push the container as web
at the time of deployment.
heroku container:push web --app discord-bot-sample-app
heroku container:release web --app discord-bot-sample-app
Redeploy as worker.
heroku container:push worker --app discord-bot-sample-app
heroku container:release worker --app discord-bot-sample-app
Certainly this didn't stop.
Recommended Posts