@@ -3,8 +3,10 @@ package org.yastech.cs50_bot
33import com.elbekd.bot.Bot
44import com.elbekd.bot.feature.chain.chain
55import com.elbekd.bot.feature.chain.jumpToAndFire
6+ import com.elbekd.bot.feature.chain.terminateChain
67import com.elbekd.bot.model.toChatId
78import com.elbekd.bot.types.KeyboardButton
9+ import com.elbekd.bot.types.Message
810import com.elbekd.bot.types.ReplyKeyboardMarkup
911import com.elbekd.bot.util.SendingByteArray
1012import org.springframework.boot.autoconfigure.SpringBootApplication
@@ -64,6 +66,9 @@ fun main(args: Array<String>)
6466 var nameEn = " "
6567 var msgID = 0L
6668
69+ var profileState = " unset"
70+ var profilePicture = URL (" https://cs50x.ir/summer" )
71+
6772 // Command Chain
6873 bot.chain(" /generate" )
6974 {
@@ -84,6 +89,103 @@ fun main(args: Array<String>)
8489 // Set User Response
8590 nameEn = " ${it.text} "
8691
92+ // Check If Use Have Profile Picture
93+ if (bot.getUserProfilePhotos(it.chat.id).photos.isNotEmpty())
94+ {
95+
96+ // Set User Image State to "SELECTIVE"
97+ profileState = " SELECTIVE"
98+
99+ // Ask User For Profile Picture
100+ bot.sendMessage(it.chat.id.toChatId(), " عکس شما در تصویر نوشته چاپ می شود . کدام تصویر را انتخاب میکنید ؟" ,
101+ replyMarkup = ReplyKeyboardMarkup ( keyboard = listOf (
102+ listOf (
103+ KeyboardButton (" عکس پروفایل" ),
104+ KeyboardButton (" ارسال عکس" ),
105+ ),
106+ listOf (KeyboardButton (" لغو" ))
107+
108+ ), oneTimeKeyboard = true /* Close After Click */ )
109+
110+ ).let { msg -> msgID = msg.messageId /* Set Promise Message ID*/ }
111+ }
112+ else
113+ {
114+
115+ // Set User Image State to "CUSTOM"
116+ profileState = " CUSTOM"
117+
118+ // Send Get Picture Message
119+ bot.sendMessage(it.chat.id.toChatId(), " لطفا عکسی میخواهید در تصویر نویسه چاپ شود را وارد کنید!" )
120+ }
121+
122+ }.then {
123+
124+ // If Profile State Is CUSTOM
125+ if (profileState == " CUSTOM" )
126+ {
127+
128+ // Set Custom Picture Url
129+ profilePicture = URL (" https://api.telegram.org/file/bot$token /${bot.getFile(it.photo[0 ].fileId).filePath} " )
130+
131+ // Jump To Final
132+ bot.jumpToAndFire(" final" , it)
133+ }
134+
135+ // If Profile State Is SELECTIVE
136+ else
137+ {
138+ when (it.text)
139+ {
140+ // On Cancel
141+ " لغو" ->
142+ {
143+ // Cancel Process
144+ removeAction(bot, it, msgID)
145+
146+ // Terminate /generate chain
147+ bot.terminateChain(it.chat.id)
148+ }
149+
150+ // On Profile Picture
151+ " عکس پروفایل" ->
152+ {
153+
154+ // Set Profile State
155+ profileState = " PROFILE_PIC"
156+
157+ // Set Profile Picture Url
158+ profilePicture = URL (
159+ " https://api.telegram.org/file/bot$token /${bot.getFile(bot.getUserProfilePhotos(it.chat.id).photos[0 ][0 ].fileId).filePath!! } "
160+ )
161+
162+ // Jump To Final
163+ bot.jumpToAndFire(" final" , it)
164+ }
165+
166+ // On Custom Picture
167+ " ارسال عکس" ->
168+ {
169+ // Set Profile State
170+ profileState = " CUSTOM"
171+
172+ // Send Get Picture Message
173+ bot.sendMessage(it.chat.id.toChatId(), " لطفا عکسی میخواهید در تصویر نویسه چاپ شود را وارد کنید!" )
174+ }
175+ }
176+ }
177+ }.then {
178+
179+ // Check If Custom Image
180+ if (profileState == " CUSTOM" ) profilePicture = URL (
181+ " https://api.telegram.org/file/bot$token /${bot.getFile(it.photo[0 ].fileId).filePath} "
182+ )
183+
184+ // Jump To Final
185+ bot.jumpToAndFire(" final" , it)
186+
187+ }.then(" final" ){
188+
87189 // Get Data Promise From User
88190 bot.sendMessage(it.chat.id.toChatId(), " نام فارسی : $nameFa \n " +
89191 " نام انگلیسی : $nameEn \n " +
@@ -92,39 +194,19 @@ fun main(args: Array<String>)
92194 KeyboardButton (" لغو" ),
93195 KeyboardButton (" ویرایش اطلاعات" ),
94196 ),
95- listOf (
96- KeyboardButton (" ساختن تصویر نوشته جدید!" )
97- )
98- ),
197+ listOf (KeyboardButton (" ساختن تصویر نوشته جدید!" ))
99198
100- // Close After Click
101- oneTimeKeyboard = true )
102- ).let { msg ->
199+ ), oneTimeKeyboard = true /* Close After Click */ )
200+
201+ ).let { msg -> msgID = msg.messageId /* Set Promise Message ID */ }
103202
104- // Set Promise Message ID
105- msgID = msg.messageId
106- }
107203 }.then {
108204
109205 // Check User Response
110206 when (it.text)
111207 {
112208 // On Cancel
113- " لغو" ->
114- {
115- // Remove Keyboard
116- bot.deleteMessage(it.chat.id.toChatId(), msgID)
117-
118- // Show Cancel Response
119- bot.sendMessage(it.chat.id.toChatId(), " عملیات با موفقیت لغو شد!" )
120- bot.sendMessage(it.chat.id.toChatId(), " دستورات ربات :\n " +
121- " \n " +
122- " /start - شروع کار با ربات\n " +
123- " /help - راهنمای ربات\n " +
124- " /generate - ساخت تصویر نویسه جدید\n " +
125- " \n " +
126- " برای پشتیبانی بات با این ایمیل در تماس باشید : [email protected] " )
127- }
209+ " لغو" -> removeAction(bot, it, msgID)
128210
129211 // On Edit Data
130212 " ویرایش اطلاعات" -> {
@@ -166,9 +248,7 @@ fun main(args: Array<String>)
166248 val g = ImageIO .read(file)
167249
168250 // Load User Profile Photo
169- val profilePhoto = ImageIO .read(
170- URL (" https://api.telegram.org/file/bot$token /${bot.getFile(bot.getUserProfilePhotos(it.chat.id).photos[0 ][0 ].fileId).filePath!! } " )
171- )
251+ val profilePhoto = ImageIO .read(profilePicture)
172252
173253 // Create User Profile Photo Buffer Image
174254 val profilePhotoOutput = BufferedImage (182 , 182 , BufferedImage .TYPE_INT_ARGB )
@@ -245,4 +325,23 @@ fun main(args: Array<String>)
245325
246326 // Start Bot
247327 bot.start()
328+ }
329+
330+ suspend fun removeAction (bot : Bot , message : Message , msgID : Long )
331+ {
332+ // Set Chat ID
333+ val chatId = message.chat.id.toChatId()
334+
335+ // Remove Keyboard
336+ bot.deleteMessage(chatId, msgID)
337+
338+ // Show Cancel Response
339+ bot.sendMessage(chatId, " عملیات با موفقیت لغو شد!" )
340+ bot.sendMessage(chatId, " دستورات ربات :\n " +
341+ " \n " +
342+ " /start - شروع کار با ربات\n " +
343+ " /help - راهنمای ربات\n " +
344+ " /generate - ساخت تصویر نویسه جدید\n " +
345+ " \n " +
346+ " برای پشتیبانی بات با این ایمیل در تماس باشید : [email protected] " )
248347}
0 commit comments