-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
210 lines (160 loc) · 7.52 KB
/
app.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/python
#-*-coding: utf-8 -*-
##from __future__ import absolute_import
###
from csv import DictWriter
from flask import Flask, jsonify, render_template, request
import json
import numpy as np
import pandas as pd
from googletrans import Translator
import webbrowser
import dropbox
import nagisa
import cutlet
from janome.tokenizer import Tokenizer
t = Tokenizer()
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,TemplateSendMessage,ImageSendMessage, StickerSendMessage, AudioSendMessage
)
from linebot.models.template import *
from linebot import (
LineBotApi, WebhookHandler
)
translator = Translator()
app = Flask(__name__)
lineaccesstoken = ' XXXXXX '
line_bot_api = LineBotApi(lineaccesstoken)
katsu = cutlet.Cutlet()
################### Dropbox ##################
dropbox_access_token = "acFejDGvB7oAAAAAAAAAAeU29LoxJ8MEQg6bGRWWBR3cHo7vMBazvFdUVgK4f-XV"
dropbox_path = "/Tbot/talk.csv"
computer_path = r"talk.csv"
client = dropbox.Dropbox (dropbox_access_token)
################### CSV ######################
headersCSV = ['Japanese','English translated']
#dict={'Japanese':'こんにちは','English translated':'Hello.'}
####################### new ########################
@app.route('/')
def index():
return "Hello World!"
@app.route('/webhook', methods=['POST'])
def callback():
json_line = request.get_json(force=False,cache=False)
json_line = json.dumps(json_line)
decoded = json.loads(json_line)
no_event = len(decoded['events'])
for i in range(no_event):
event = decoded['events'][i]
event_handle(event)
return '',200
def event_handle(event):
print(event['type'])
if event['type'] == "memberJoined":
#userId =
#userId =
profile = line_bot_api.get_profile(event['joined']['members'][0]['userId'])
print(profile.display_name)
#line_bot_api.push_message(event['source']['groupId'], TextSendMessage(text='Welcome'+profile.display_name))
#print(type(userId))
Welcomemsg = "Welcome "+profile.display_name+" to our group!😆🥳\nPlease add me as friend so I can start translate text for you!🤓\nType '/Help' for more information\n\nこんにちは "+profile.display_name+"さん!😆🥳\n友達になってください、そうすれば私はあなたのためにテキストを翻訳することができます。🤓\n詳細は「/Help」と入力してください。"
line_bot_api.push_message(event['source']['groupId'], TextSendMessage(text= Welcomemsg))
print('Someone Joined')
if event['type'] == "memberLeft":
profile = line_bot_api.get_profile(event['left']['members'][0]['userId'])
#line_bot_api.push_message(event['source']['groupId'], TextSendMessage(text='Bye'+profile.display_name))
byemsg = "So long "+profile.display_name+"\nHope we can meet again😢\n\nバイバイ"+profile.display_name+"さん\nまたお会いできることを願っています。😢"
line_bot_api.push_message(event['source']['groupId'], TextSendMessage(text=byemsg))
print('Someone Left')
try:
userId = event['source']['userId']
except:
print('error cannot get userId')
return ''
return ''
try:
groupId = event['source']['groupId']
except:
print('error cannot get userId')
return ''
try:
rtoken = event['replyToken']
except:
print('error cannot get rtoken')
return ''
try:
msgId = event["message"]["id"]
msgType = event["message"]["type"]
except:
print('error cannot get msgID, and msgType')
sk_id = np.random.randint(1,17)
replyObj = StickerSendMessage(package_id=str(1),sticker_id=str(sk_id))
line_bot_api.reply_message(rtoken, replyObj)
return ''
if msgType == "text":
profile = line_bot_api.get_profile(userId)
group = line_bot_api.get_group_summary(groupId)
group_count = str(line_bot_api.get_group_members_count(groupId))
profile.display_name
msg = str(event["message"]["text"])
translation = translator.translate(msg)
words = nagisa.tagging(msg)
wordx = ""
wordslength = len(words.words)
for x in range(wordslength):
if len(words.words[x]) == 1:
wordx = wordx + words.words[x] + "\t\t "+ words.postags[x]+"\n"
elif len(words.words[x]) == 2:
wordx = wordx + words.words[x] + "\t "+ words.postags[x]+"\n"
elif len(words.words[x]) == 3:
wordx = wordx + words.words[x] + "\t"+ words.postags[x]+"\n"
if msg == '/Download':
client.files_delete(dropbox_path)
client.files_upload (open (computer_path, "rb"). read (), dropbox_path)
print ("upload: {}" .format (computer_path))
link_to_download= client.sharing_create_shared_link(dropbox_path)
replyObj = TextSendMessage(text="Dropbox Link: "+ link_to_download.url)
elif msg == '/Help':
helpmsg = "- Text in Japanese will be translated to English\n- Text in English will be translated to Japanese\n- Type '/Download' to download the conversation\n- Type '/Clear' to delete all conversation"
replyObj = TextSendMessage(text=helpmsg)
elif msg == '/Clear':
talk = open("talk.csv", "w")
talk.truncate()
talk.close()
with open('talk.csv', 'w') as open_file:
dw = DictWriter(open_file, delimiter=',', fieldnames=headersCSV)
dw.writeheader()
replyObj = TextSendMessage(text='Cleared all conversation successfully!')
elif translation.src == 'en':
translation = translator.translate(msg, dest='ja')
replyObj = TextSendMessage(text="翻訳 🇺🇸 => 🇯🇵 \n\n"+profile.display_name+"さんは\n 「"+translation.text+"」 \nと言った\n\n"+ wordx)
df = pd.read_csv('talk.csv')
print(df.to_string())
elif translation.src == 'ja':
translation = translator.translate(msg, dest='en')
replyObj = TextSendMessage(text="Translation 🇯🇵 => 🇺🇸 \n\n"+profile.display_name+" said\n '"+translation.text+"\nRomanji\n "+katsu.romaji(msg)+"'\n\n"+wordx)
#print(katsu.romaji(msg))
dict={'Japanese':msg,'English translated':translation.text}
with open('talk.csv', 'a', newline='') as talk:
dictwriter_object = DictWriter(talk, fieldnames=headersCSV)
dictwriter_object.writerow(dict)
talk.close()
#f.write(dict)
df = pd.read_csv('talk.csv')
print(df.to_string())
try:
line_bot_api.reply_message(rtoken, replyObj)
print("Translate and Reply Successfuly")
except :
confused = ['I have no idea what you are saying','Please Check your spelling','😵😵😵']
rand = np.random.randint(0,2)
replyObj = TextSendMessage(text=confused[rand])
line_bot_api.reply_message(rtoken, replyObj)
print("Translate and Reply Failed")
else:
sk_id = np.random.randint(1,17)
replyObj = StickerSendMessage(package_id=str(1),sticker_id=str(sk_id))
line_bot_api.reply_message(rtoken, replyObj)
return ''
if __name__ == '__main__':
app.run(debug=True)