Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F665134
telegram_room.ex
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
telegram_room.ex
View Options
defmodule
LSG.TelegramRoom
do
require
Logger
@behaviour
Telegram.ChatBot
alias
Telegram.Api
@impl
Telegram.ChatBot
def
init
(
id
)
do
token
=
Keyword
.
get
(
Application
.
get_env
(
:lsg
,
:telegram
,
[]),
:key
)
{
:ok
,
chat
}
=
Api
.
request
(
token
,
"getChat"
,
chat_id
:
id
)
Logger
.
debug
(
"Starting ChatBot for room
#{
id
}
\"
#{
chat
[
"title"
]
}
\""
)
[
net
,
chan
]
=
String
.
split
(
chat
[
"title"
],
"/"
,
parts
:
2
)
case
IRC.Connection
.
get_network
(
net
,
chan
)
do
%
IRC.Connection
{}
->
:global
.
register_name
({
__MODULE__
,
net
,
chan
},
self
())
{
:ok
,
_
}
=
Registry
.
register
(
IRC.PubSub
,
"
#{
net
}
/
#{
chan
}
:message"
,
plugin
:
__MODULE__
)
{
:ok
,
_
}
=
Registry
.
register
(
IRC.PubSub
,
"
#{
net
}
/
#{
chan
}
:triggers"
,
plugin
:
__MODULE__
)
err
->
Logger
.
warn
(
"Did not found telegram match for
#{
id
}
\"
#{
chat
[
"title"
]
}
\""
)
end
{
:ok
,
%{
id
:
id
,
net
:
net
,
chan
:
chan
}}
end
def
handle_update
(%{
"message"
=>
%{
"from"
=>
%{
"id"
=>
user_id
},
"text"
=>
text
}},
_token
,
state
)
do
account
=
IRC.Account
.
find_meta_account
(
"telegram-id"
,
user_id
)
connection
=
IRC.Connection
.
get_network
(
state
.
net
)
IRC.PuppetConnection
.
send_message
(
account
,
connection
,
state
.
chan
,
text
)
{
:ok
,
state
}
end
def
handle_update
(
update
,
token
,
state
)
do
{
:ok
,
state
}
end
def
handle_info
({
:irc
,
_
,
%
IRC.Message
{
sender
:
%{
nick
:
nick
},
text
:
text
}},
state
)
do
LSG.Telegram
.
send_message
(
state
.
id
,
"<
#{
nick
}
>
#{
text
}
"
)
{
:ok
,
state
}
end
def
handle_info
({
:raw
,
lines
},
state
)
when
is_list
(
lines
)
do
formatted
=
for
l
<-
lines
,
into
:
<<>>,
do
:
l
<>
"
\n
"
LSG.Telegram
.
send_message
(
state
.
id
,
formatted
)
{
:ok
,
state
}
end
def
handle_info
({
:raw
,
line
},
state
)
do
handle_info
({
:raw
,
[
line
]},
state
)
end
def
handle_info
(
info
,
state
)
do
{
:ok
,
state
}
end
end
File Metadata
Details
Attached
Mime Type
text/x-ruby
Expires
Fri, Feb 27, 10:51 AM (1 d, 21 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
87174
Default Alt Text
telegram_room.ex (1 KB)
Attached To
rNOLA Nola
Event Timeline
Log In to Comment