Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions 行銷實用工具/Linebot-Basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Line Bot Python 程式碼

這是一個使用 Python 和 Flask 框架,以及 Line Messaging API 來建立 Line Bot 的範例程式碼。

## 環境設定

首先,你需要在你的 LINE Developers 控制台中建立一個新的 Messaging API 服務,並取得 Channel Access Token 和 Channel Secret。

然後,將這些資訊填入程式碼中對應的位置:

```python
# 必須放上自己的Channel Access Token
line_bot_api: LineBotApi = LineBotApi('你自己的token')
# 必須放上自己的Channel Secret
handler: WebhookHandler = WebhookHandler('你自己的secret')
```

## 執行程式碼

你可以在本地或者伺服器上執行這個程式碼。如果你在本地運行,你可能需要使用 ngrok 或其他類似的工具來建立一個公開的網址,以便 Line 服務能夠訪問你的 webhook。

## 錯誤處理

這個程式碼包含了基本的錯誤處理。如果在處理 Line 事件或發送訊息時出現錯誤,程式碼會捕獲這個錯誤並打印出錯誤的詳細信息。

## 進一步學習

如果你想要進一步學習如何使用 Line Messaging API,你可以參考 [LINE Messaging API SDK for Python](https://github.com/line/line-bot-sdk-python) 的官方文件。
66 changes: 39 additions & 27 deletions 行銷實用工具/Linebot-Basic/app.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,62 @@
#載入LineBot所需要的套件
# 載入LineBot所需要的套件
from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError, LineBotApiError
from linebot.models import MessageEvent, TextMessage, TextSendMessage

from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *

app = Flask(__name__)
app: Flask = Flask(__name__)

# 必須放上自己的Channel Access Token
line_bot_api = LineBotApi('你自己的token')
line_bot_api: LineBotApi = LineBotApi('你自己的token')
# 必須放上自己的Channel Secret
handler = WebhookHandler('你自己的secret')
handler: WebhookHandler = WebhookHandler('你自己的secret')

line_bot_api.push_message('你自己的ID', TextSendMessage(text='你可以開始了'))
try:
line_bot_api.push_message('你自己的ID', TextSendMessage(text='你可以開始了'))
except LineBotApiError as e:
# 錯誤處理
print(e.status_code)
print(e.error.message)
print(e.error.details)

# 監聽所有來自 /callback 的 Post Request
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']

# get request body as text
body = request.get_data(as_text=True)
def callback() -> str:
"""
監聽所有來自 /callback 的 Post Request
"""
# 取得 X-Line-Signature 標頭值
signature: str = request.headers['X-Line-Signature']

# 取得 request body 文字
body: str = request.get_data(as_text=True)
app.logger.info("Request body: " + body)

# handle webhook body
# 處理 webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
print("無效的簽名。請檢查你的 channel access token/channel secret。")
abort(400)

return 'OK'

#訊息傳遞區塊
##### 基本上程式編輯都在這個function #####
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
message = TextSendMessage(text=event.message.text)
line_bot_api.reply_message(event.reply_token,message)
def handle_message(event: MessageEvent):
"""
訊息傳遞區塊
基本上程式編輯都在這個function
"""
try:
message: TextSendMessage = TextSendMessage(text=event.message.text)
line_bot_api.reply_message(event.reply_token,message)
except LineBotApiError as e:
# 錯誤處理
print(e.status_code)
print(e.error.message)
print(e.error.details)

#主程式
import os
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
port: int = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
12 changes: 2 additions & 10 deletions 行銷實用工具/Linebot-Basic/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
line-bot-sdk
bs4
flask
pymongo
datetime
pandas
SnowNLP
emoji
pyshorteners
scipy
line-bot-sdk==3.5.0
flask==3.0.0
17 changes: 17 additions & 0 deletions 行銷實用工具/telegramBot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 使用官方 Python 基礎映像
FROM python:3.11.5-slim

# 設定工作目錄
WORKDIR /app

# 將當前目錄的內容複製到工作目錄中
COPY . /app

# 安裝所需的套件
RUN pip install --no-cache-dir -r requirements.txt

# 讓外部連線可以存取到 Flask 應用程式
EXPOSE 5000

# 啟動 Flask 應用程式
CMD ["python", "app.py"]
36 changes: 36 additions & 0 deletions 行銷實用工具/telegramBot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# telegramBot

這是一個使用 Flask 和 Telegram API 建立的機器人應用程式。

## 專案結構

```
.
├── Dockerfile
├── docker-compose.yml
└── app.py
```

## 檔案說明

- `app.py`:這是主要的應用程式檔案,包含了 Flask 應用程式的初始化和路由設定,以及 Telegram 機器人的設定和訊息處理。
- `Dockerfile`:這個檔案定義了 Docker 映像的建立過程,包括基礎映像的選擇、工作目錄的設定、應用程式檔案的複製、套件的安裝和應用程式的啟動。
- `docker-compose.yml`:這個檔案定義了 Docker 容器的配置,包括映像的建立、端口的映射和環境變數的設定。

## 使用說明

1. 先確保你的電腦已經安裝了 Docker 和 Docker Compose。
2. 將你的 Telegram 機器人 token 填入 `app.py` 中的對應位置。
3. 在專案根目錄下執行 `docker-compose up` 命令來啟動應用程式。
4. 你的應用程式現在應該已經在 http://localhost:5000/ 運行了。

## 注意事項

- 如果你在本地運行應用程式,請確保 5000 端口沒有被其他應用程式佔用。
- 請確保你的 Telegram 機器人 token 是正確的,否則機器人將無法正常工作。

## 開發環境

- Python 3.11.5
- Flask
- Telegram API
52 changes: 30 additions & 22 deletions 行銷實用工具/telegramBot/app.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
import os
import telegram
from flask import Flask, request
from telegram.ext import Dispatcher, MessageHandler, Filters, CallbackContext, CallbackQueryHandler
from telegram import InlineKeyboardMarkup, InlineKeyboardButton, Bot, Update, KeyboardButton, ReplyKeyboardMarkup
from telegram import Bot, Update
from telegram.ext import Dispatcher, MessageHandler, Filters, CallbackContext

# Initial Flask app
app = Flask(__name__)
# 初始化 Flask 應用
app: Flask = Flask(__name__)

# 設定你的token
bot = telegram.Bot(token=('你的token'))
bot.send_message(chat_id = '你的ID', text ='你可以開始了')
# 設定你的 token
bot: Bot = Bot(token='你的 token')

@app.route('/hook', methods=['POST'])
def webhook_handler():
"""Set route /hook with POST method will trigger this method."""
def webhook_handler() -> str:
"""
當路由 /hook 使用 POST 方法時,將觸發此方法。

Returns:
str: 回傳 'ok' 字串
"""
if request.method == "POST":
update = telegram.Update.de_json(request.get_json(force=True), bot)
update: Update = Update.de_json(request.get_json(force=True), bot)

# Update dispatcher process that handler to process this message
# 更新調度器處理程序以處理此訊息
dispatcher.process_update(update)
return 'ok'


def reply_handler(update: Update, _: CallbackContext):
"""自動回復"""
def reply_handler(update: Update, _: CallbackContext) -> None:
"""
回覆處理程序。

Args:
update (Update): 從使用者接收到的更新訊息
_ (CallbackContext): 回呼上下文,此處不使用,所以命名為 "_"
"""
user = update.message.from_user
userText = update.message.text
update.message.reply_text(userText)
user_text = update.message.text
update.message.reply_text(user_text)

# New a dispatcher for bot
dispatcher = Dispatcher(bot, None)
# 為機器人新建一個調度器
dispatcher: Dispatcher = Dispatcher(bot, None, use_context=True)

# Add handler for handling message, there are many kinds of message. For this handler, it particular handle text
# message.
# 為調度器添加處理程序,處理各種類型的訊息。此處的處理程序專門處理文字訊息。
dispatcher.add_handler(MessageHandler(Filters.text, reply_handler))

if __name__ == "__main__":
# Running server
port = int(os.environ.get('PORT', 27017))
# 運行伺服器
port: int = int(os.environ.get('PORT', 27017))
app.run(host='0.0.0.0', port=port)
8 changes: 8 additions & 0 deletions 行銷實用工具/telegramBot/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.8'
services:
app:
build: .
ports:
- "5000:5000"
environment:
- FLASK_ENV=development
4 changes: 2 additions & 2 deletions 行銷實用工具/telegramBot/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
certifi
chardet
click
flask
flask==2.3.3
future
gunicorn
idna
itsdangerous
jinja2
markupsafe
python-telegram-bot
python-telegram-bot==20.6
requests
urllib3
werkzeug
Expand Down