/** * Summary. * * @since 0.9.0 * @see * * @param \Symfony\Component\HttpFoundation\Request $request * @param \Silex\Application $app * * @return JsonResponse * @author nguyenvanduocit */ public function post(Request $request, Application $app) { $sign = $request->get('sign'); $sign = strtolower($sign); if (!$sign) { $errorResponse = new Response(); $errorResponse->setErrorCode('100'); $errorResponse->setText('Your have to provice the sign.'); return new JsonResponse($errorResponse); } if (!array_key_exists($sign, $this->dailyEndPoint)) { $errorResponse = new Response(); $errorResponse->setErrorCode('101'); $errorResponse->setText('Your sign is not exist. What planet are you come from?'); return new JsonResponse($errorResponse); } $response = new AttachmentResponse(); $holoscope = $this->getDailyHoroscope($sign); $response->setText($holoscope->getContent()); $response->setThumbUrl($holoscope->getImageURL()); $response->setTitle($holoscope->getName()); // Set Field $fields = $holoscope->getRating(); array_walk($fields, function (&$item) { $item['short'] = 'true'; }); $response->setFields($fields); return new JsonResponse($response); }
/** * Summary. * * @since 0.9.0 * @see * * @param \Symfony\Component\HttpFoundation\Request $request * @param \Silex\Application $app * * @return JsonResponse * @author nguyenvanduocit */ public function post(Request $request, Application $app) { $category = $request->get('category', 'all'); $response = new AttachmentResponse(); $response->setType('text'); if (array_key_exists($category, $this->quotes)) { $quotes = $this->quotes[$category]; $quoteContent = $quotes[array_rand($quotes)]; $response->setText($quoteContent); } else { $categorylist = implode(', ', array_keys($this->quotes)); $response->setText('I don\'t know about this category, I only know abount ' . $categorylist); } return new JsonResponse($response); }
/** * Summary. * * @since 0.9.0 * @see * * @param \Symfony\Component\HttpFoundation\Request $request * @param \Silex\Application $app * * @return JsonResponse * @author nguyenvanduocit */ public function post(Request $request, Application $app) { $category = $request->get('category', 'all'); /** * Todo support another fact */ $response = new AttachmentResponse(); if ($category === 'programming') { $data = $this->getRandomFactOfProgramming(); $response->setTitle($data['title']); $response->setTitleLink('http://laptrinh.senviet.org'); $response->setImageUrl($data['image_url']); } else { $categorylist = implode(', ', array_keys($this->images)); $response->setText('I don\'t know about this category, I only know abount ' . $categorylist); } return new JsonResponse($response); }
/** * Summary. * * @since 0.9.0 * @see * * @param \Symfony\Component\HttpFoundation\Request $request * @param \Silex\Application $app * * @return \Symfony\Component\HttpFoundation\JsonResponse * @author nguyenvanduocit */ public function translate(Request $request, Application $app) { $resultObject = new AttachmentResponse(); $from = $request->get('from'); $to = $request->get('to', 'en'); $text = $request->get('text'); $translator = new TranslateClient($from, $to); $translatedText = $translator->translate($text); if ($translatedText) { $from = $from ? $from : $translator->getLastDetectedSource(); $messageFormat = '%1$s -> %2$s : %3$s'; $message = sprintf($messageFormat, $from, $to, $translatedText); $resultObject->setText($message); } else { $resultObject->setErrorCode(404); $detectedSource = $translator->getLastDetectedSource(); if (!$detectedSource) { $resultObject->setText('Can not detect your language'); } else { $resultObject->setText('Can not translate your text.'); } } return new JsonResponse($resultObject); }
/** * Summary. * * @since 0.9.0 * @see * * @param \Symfony\Component\HttpFoundation\Request $request * @param \Silex\Application $app * * @return JsonResponse * @author nguyenvanduocit */ public function generate(Request $request, Application $app) { $backgroundId = $request->get('backgroundId'); $resultObject = new AttachmentResponse(); if (array_key_exists($backgroundId, $this->memeList)) { $text = $request->get('text'); $meme = $this->memeList[$backgroundId]; $fileName = md5(mt_rand(1, 20)) . '.jpg'; $mainLayer = ImageWorkshop::initFromPath($meme['src']); $textPaths = explode(';', $text); foreach ($textPaths as $index => $text) { if (array_key_exists($index, $meme['position'])) { $position = $meme['position'][$index]; } else { $lastPostion = count($meme['position']) - 1; $position = $meme['position'][$lastPostion]; } $textLayer = ImageWorkshop::initTextLayer($text, APP_DIR . '/Asset/font/OpenSans-Bold.ttf', $meme['font']['size'], $meme['font']['color'], 0, null); if ($textLayer->getWidth() >= $mainLayer->getWidth()) { $textLayer->resizeInPixel($mainLayer->getWidth() - 100, NULL, TRUE); } if ($position === 'top') { $y = 0 + 20; } else { $y = $mainLayer->getHeight() - $textLayer->getHeight() - 20; } $x = $mainLayer->getWidth() / 2 - $textLayer->getWidth() / 2; $mainLayer->addLayer($index, $textLayer, $x, $y); } $mainLayer->save($this->outputDir, $fileName); $resultObject->setImageUrl('http://slackbotapi.senviet.org/web/public/meme/' . $fileName . '?rand=' . uniqid('rand', FALSE)); } else { $resultObject->setImageUrl('http://slackbotapi.senviet.org/web/public/meme/list.jpg'); } return new JsonResponse($resultObject); }