-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
121 lines (101 loc) · 3.47 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
r"""
Life's pathetic, have fun ("▔□▔)/hi~♡ Nasy.
Excited without bugs::
| * *
| . .
| .
| * ,
| .
|
| *
| |\___/|
| ) -( . ·
| =\ - /=
| )===( *
| / - \
| |- |
| / - \ 0.|.0
| NASY___\__( (__/_____(\=/)__+1s____________
| ______|____) )______|______|______|______|_
| ___|______( (____|______|______|______|____
| ______|____\_|______|______|______|______|_
| ___|______|______|______|______|______|____
| ______|______|______|______|______|______|_
| ___|______|______|______|______|______|____
author : Nasy https://nasy.moe
date : Apr 3, 2019
email : Nasy <nasyxx+python@gmail.com>
filename : bot.py
project : words_bot
license : GPL-3.0+
There are more things in heaven and earth, Horatio, than are dreamt.
-- From "Hamlet"
"""
# Other Packages
from jieba import lcut as jc
from jieba import initialize
from pkuseg import pkuseg
from thulac import thulac
from telegram import InputTextMessageContent, InlineQueryResultArticle
from telegram.ext import (Updater, CommandHandler,
CallbackContext, InlineQueryHandler,)
from telegram.update import Update
from config import BOT_TOKEN
initialize()
tc = thulac(seg_only=True).cut # pylint: disable=C0103
pc = pkuseg().cut # pylint: disable=C0103
def words(update: Update, context: CallbackContext) -> None:
"""Words the inline message."""
query = update.inline_query.query
resj = " ".join(jc(query, cut_all=False))
rest = tc(query, text=True)
resp = " ".join(pc(query))
print("--" * 10)
print(query, end="\n")
print("-" * 10)
print("jieba: " + resj, "thulac: " + rest, "pkuseg: " + resp, sep="\n")
print("--" * 10, end="\n\n")
context.bot.answer_inline_query(
update.inline_query.id,
[
InlineQueryResultArticle(
id=query[:5] + "|j",
title="jieba",
description=resj,
input_message_content=InputTextMessageContent(resj),
),
InlineQueryResultArticle(
id=query[:5] + "|t",
title="thulac",
description=rest,
input_message_content=InputTextMessageContent(rest),
),
InlineQueryResultArticle(
id=query[:5] + "|p",
title="pkuseg",
description=resp,
input_message_content=InputTextMessageContent(resp),
),
],
)
def start_cmd(update: Update, context: CallbackContext) -> None:
"""Start command."""
context.bot.send_message(
chat_id=update.message.chat_id,
text="Nasy Words Bot\n"
" -- Help you break a Chinese string into words.\n"
" -- It joins words with separating spaces.\n"
" -- inline bot\n"
" -- https://github.com/nasyxx/nasy-TG-words-bot",
)
def main() -> None:
"""Main function."""
bot = Updater(token=BOT_TOKEN, use_context=True)
dispatcher = bot.dispatcher
dispatcher.add_handler(CommandHandler("start", start_cmd))
dispatcher.add_handler(InlineQueryHandler(words))
bot.start_polling()
if __name__ == "__main__":
main()