-
Notifications
You must be signed in to change notification settings - Fork 0
Basics_BasicBot
Note
Please read the "Getting Started Guide" in order to learn more smoothly.
In this chapter, we'll learn how to make a simple LINE bot that is able to:
- Know its own name & profile
- Do something on startup
- Handle text messages
- Sending simple messages (in
str
)
In addition, you'll also learn:
- How to use
Client
- Context & type hints
Without further ado, let's just get started!
First things first, we'll need to import the requisite module — linex. We'll be importing the following:
-
Client
: Represents the LINE bot (most important) -
TextMessageContext
: The text message context for type hints.
from linex import Client, TextMessageContext
Next up, let's define our client. The Client
class represents our LINE Bot, and requires two key elements: the channel secret and the channel access token.
Those elements are like the bread and the hotdog. Without the bread, I cannot even consume the dawg 😱; without the hotdog, it's just some plain old bread, boring.
And that's channel secret and channel access token. They need to be there together to make the bot connect and talk!
client = Client(
"channel secret", # the bread
"channel access token" # the dawwg
)
WAIT! But what if SOMEONE wants to steal MY hotdog???
Good question.
How can we even avoid it? I mean, if you're using online editors like Replit that make your project publicly available, your channel secret and access token will ALSO be publicy visible.
But if you're working locally, it's basically fine — you can skip this part.
WHO DARE STEALS MY HOTDOG?!
That's no good. We don't want anyone from nowhere to steal our bot and do whatever they want.
That's when environment variables come in handy. Using environment variables is just like eating your hotdog in your own private room. No one can interrupt you or steal it from you.
But, hold on... I don't have a private room! I'm basically "roomless."
In that case, we need to rent one. And by renting, I mean setting environment variables.
If you're working on Replit, you can use the Secrets tab to store environment variables, in a cool way. Please set two keys:
-
LINEX_CHANNEL_SECRET
: Your channel secret. -
LINEX_CHANNEL_ACCESS_TOKEN
: Your channel access token.
As for other linux-based environments:
export LINEX_CHANNEL_SECRET="channel secret"
export LINEX_CHANNEL_ACCESS_TOKEN="channel access token"
Now, change your code to the following, without adding channel secrets or channel access token:
client = Client() # linex reads them for you
With environment variables out of the way, let's get back to coding.
Let's talk about handlers. Handlers, or decorators, are used in LineX for handling events such as ready
, text
and many more.
To get started, let's make a simple handler for the ready
event — it is called when the bot is ready.
@client.event
async def on_ready():
print("I am ready!")
That's really basic, and doesn't do much than just printing a fixed message. I'm sobbing rn. 😭😭
Therefore, why not give it a little twist? How about getting a little more advanced — printing the bot's profile?
Now that's more fun!
Remember the Client
class we used earlier? There's an attribute named "user," it simply represents the bot user itself. By accessing the attribute, you can retrive the bot's handle, name, and more. Let's just get its name and handle for now.
Note
By "handle" I meant the basic ID (@bot-id
) if you're more familiar with the LINE Official API. And yes, you can use basic_id
instead of handle
. handle
is just an alias.
You can use the attribute like so:
@client.event
async def on_ready():
my = client.user
print(
my.name,
my.handle
)
In the above code, we assigned a variable (named my
) with the client.user
as its value, and use the print
function to log the bot's name and handle onto the console.
Yes, that's cool. But what we're doing here is just ugh, receiving ready events. Are bots THAT useless?? 😤😤🥶
The on_ready
event is kind of boring at this point, since we cannot directly interact with the user.
That's when the on_text
event joins the battle.
Here's a silly game: what does Discord and LINE have in common?
- (A): Cookies & milk for Santa
- (B): Friends who are last online 7 years ago
- (C): Unusual crash that just occurs whenever it wants to
- (D): A brand that has many games and sponsorships for some reason
Time's up! You're wrong; it's messages:tm:.
LINE and Discord, to be frank, is just all about sending messages:tm:.
Without them:tm:, nothing doesn't really make sense: LINE would be a company that makes games and has a virtual banking system (which is odd), and Discord would sell Nitro for nothing in return.
Now you've understood the importance of messages:tm:, let's talk about text messages:tm: for our LINE bot!
When our bot recieves a text message for a user, a text
event will occur, which you can handle it by simply using on_text
.
But HOLD ON! Our handler function now requires an argument — the context, or ctx
.
The ctx
argument stores a bunch of this on-going event, such as the user interacting with the bot, the group chat the bot is in, the message content, and many more.
Additionally, the ctx
argument is the (one and only) key for replying the user.