日誌2022-06-11 00:55
Line Bot研究 - Flex Message應用&結合twitter API作者:熾炎之翼
if '.jpg' in get_message.lower() or '.png' in get_message.lower(): if split[-1].isdigit(): n = int(split[-1]) if(n > 10): n = 10 else: n = 1 URL_list = [] params = { "engine": "google", "tbm": "isch" } try: params['q'] = get_message params['api_key'] = random.choice(jdata['serpapi_key']) client = GoogleSearch(params) data = client.get_dict() while('error' in data.keys()): params['api_key'] = random.choice(jdata['serpapi_key']) client = GoogleSearch(params) data = client.get_dict() imgs = data['images_results'] x = 0 if(n > len(imgs)): n = len(imgs) for img in imgs: if x < n and img['original'][-4:].lower() in ['.jpg', '.png', 'jpeg'] and img['original'][:5] == 'https': URL_list.append(img['original']) x += 1 with open('json/imgBubble.json', 'r', encoding='utf8') as jfile: jdata = json.load(jfile) ctn = [] for i in range(n): tmp = copy.deepcopy(jdata) tmp['hero']['url'] = tmp['hero']['action']['uri'] = URL_list[i] ctn.append(tmp) if len(ctn) > 1: with open('json/carousel.json', 'r', encoding='utf8') as jfile: jdataCtn = json.load(jfile) jdataCtn['contents'] = ctn reply = jdataCtn line_bot_api.reply_message(event.reply_token,FlexSendMessage('imgs',reply)) else: img_reply(ctn[0]['hero']['url']) except: url = 'https://www.google.com.tw/search?q=' + get_message + '&tbm=isch' request = requests.get(url=url) html = request.content bsObj = BeautifulSoup(html, 'html.parser') content = bsObj.findAll('img', {'class': 't0fcAb'}) for i in content: URL_list.append(i['src']) url = random.choice(URL_list) img_reply(url) |
{ "type": "bubble", "hero": { "type": "image", "url": "", "action": { "type": "uri", "uri": "" }, "size": "full" } } |
if len(ctn) > 1: with open('json/carousel.json', 'r', encoding='utf8') as jfile: jdataCtn = json.load(jfile) jdataCtn['contents'] = ctn reply = jdataCtn else: img_reply(ctn[0]['hero']['url']) |
{ "type": "carousel", "contents": [] } |
if 'twitter.com' in get_message: urlElement = get_message.split('/') auth = tweepy.OAuthHandler(os.environ.get( "TWITTER_APP_KEY"), os.environ.get("TWITTER_APP_SECRET")) auth.set_access_token(os.environ.get( "TWITTER_ACCESS_TOKEN"), os.environ.get("TWITTER_ACCESS_TOKEN_SECRET")) api = tweepy.API(auth) try: tweet = api.get_status(urlElement[-1], tweet_mode="extended") url = tweet.extended_entities["media"][0]["video_info"]["variants"][0]["url"].split('?')[ 0] url2 = tweet.extended_entities["media"][0]['media_url'] if('https' not in url2): url2 = url2.replace('http', 'https') video_reply(url, url2) except: tweet = api.get_status(urlElement[-1]) with open('json/carousel.json', 'r', encoding='utf8') as jfile: jdata = json.load(jfile) with open('json/twitterBubble.json', 'r', encoding='utf8') as jfile: jdata1 = json.load(jfile) with open('json/imgBubble.json', 'r', encoding='utf8') as jfile: jdata2 = json.load(jfile) ctn = [] jdata1['body']['contents'][0]['url'] = tweet.user.profile_image_url.replace( 'http', 'https') jdata1['body']['contents'][1]['text'] = tweet.user.name jdata1['body']['contents'][2]['text'] = '@'+tweet.user.screen_name jdata1['body']['contents'][3]['contents'][1]['text'] = str( tweet.user.followers_count) jdata1['body']['contents'][5]['contents'][0]['text'] = tweet.text jdata1['body']['contents'][5]['contents'][2]['contents'][1]['text'] = str( tweet.retweet_count) jdata1['body']['contents'][5]['contents'][3]['contents'][1]['text'] = str( tweet.favorite_count) ctn.append(jdata1) tweet = api.get_status(urlElement[-1], tweet_mode="extended") if 'media' in tweet.entities: for media in tweet.extended_entities['media']: tmp = copy.deepcopy(jdata2) if('https' not in media['media_url']): tmp['hero']['url'] = tmp['hero']['action']['uri'] = media['media_url'].replace( 'http', 'https') else: tmp['hero']['url'] = tmp['hero']['action']['uri'] = media['media_url'] ctn.append(tmp) if len(ctn) > 1: with open('json/carousel.json', 'r', encoding='utf8') as jfile: jdataCtn = json.load(jfile) jdataCtn['contents'] = ctn reply = jdataCtn else: reply = jdata1 line_bot_api.reply_message( event.reply_token, FlexSendMessage('tweet', reply)) |
auth = tweepy.OAuthHandler(os.environ.get("TWITTER_APP_KEY"), os.environ.get("TWITTER_APP_SECRET")) auth.set_access_token(os.environ.get("TWITTER_ACCESS_TOKEN"), os.environ.get("TWITTER_ACCESS_TOKEN_SECRET")) api = tweepy.API(auth) |
def video_reply(URL, URL2): reply = VideoSendMessage( original_content_url=URL, preview_image_url=URL2) line_bot_api.reply_message(event.reply_token, reply) |
{ "type": "bubble", "body": { "type": "box", "layout": "vertical", "contents": [{ "type": "image", "url": "", "position": "absolute", "size": "xxs", "offsetEnd": "xxl", "offsetTop": "xxl" }, { "type": "text", "text": "", "weight": "bold", "size": "xl", "margin": "md", "color": "#00acee" }, { "type": "text", "text": "", "weight": "bold", "color": "#aaaaaa", "size": "xs" }, { "type": "box", "layout": "horizontal", "contents": [{ "type": "text", "text": "followers:", "size": "xxs", "color": "#aaaaaa" }, { "type": "text", "text": "", "size": "xxs" } ], "spacing": "none", "margin": "none", "width": "100px" }, { "type": "separator", "margin": "xxl" }, { "type": "box", "layout": "vertical", "margin": "xxl", "spacing": "sm", "contents": [{ "type": "text", "text": "", "size": "md", "wrap": true }, { "type": "separator", "margin": "xxl" }, { "type": "box", "layout": "horizontal", "margin": "xxl", "contents": [{ "type": "text", "text": "RETWEETS:", "size": "xxs", "color": "#555555" }, { "type": "text", "text": "", "size": "sm", "color": "#111111", "align": "end" } ] }, { "type": "box", "layout": "horizontal", "contents": [{ "type": "text", "text": "LIKES", "size": "xxs", "color": "#555555" }, { "type": "text", "text": "", "size": "sm", "color": "#111111", "align": "end" } ] } ] }, { "type": "separator", "margin": "xxl" } ] }, "styles": { "footer": { "separator": true } } } |
2022-06-11 01:05熾炎之翼:欸恭喜 line API不讓bot發gif檔
最多只能貼連結ㄟ
2022-06-11 10:56熾炎之翼:讚!
2022-06-11 10:56熾炎之翼:https://media.discordapp.net/attachments/856516846144192543/985014446466408448/IMG_8040.jpg