last module update ...
This commit is contained in:
@@ -0,0 +1,673 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="fa" dir="rtl">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>راهنمای جامع ساخت بات بله با C# | پیادهسازی Bot API</title>
|
||||
<style>
|
||||
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
|
||||
:root {
|
||||
--primary: #1a365d;
|
||||
--primary-light: #2c5282;
|
||||
--accent: #c9a961;
|
||||
--accent-dark: #a8893f;
|
||||
--bg: #faf9f6;
|
||||
--card-bg: #ffffff;
|
||||
--text: #1a202c;
|
||||
--text-muted: #4a5568;
|
||||
--border: #e2e8f0;
|
||||
--success: #38a169;
|
||||
--danger: #e53e3e;
|
||||
--warning: #dd6b20;
|
||||
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
|
||||
}
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body {
|
||||
font-family: 'Vazirmatn', Tahoma, sans-serif;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
line-height: 1.9;
|
||||
font-size: 15px;
|
||||
padding: 30px 15px;
|
||||
}
|
||||
.document-container {
|
||||
max-width: 950px;
|
||||
margin: 0 auto;
|
||||
background: var(--card-bg);
|
||||
box-shadow: var(--shadow);
|
||||
border-radius: 12px;
|
||||
overflow: hidden;
|
||||
}
|
||||
.document-header {
|
||||
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
|
||||
color: white;
|
||||
padding: 50px 40px 40px;
|
||||
position: relative;
|
||||
border-bottom: 4px solid var(--accent);
|
||||
}
|
||||
.document-header::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0; right: 0;
|
||||
width: 200px; height: 200px;
|
||||
background: radial-gradient(circle, rgba(201,169,97,0.15) 0%, transparent 70%);
|
||||
border-radius: 50%;
|
||||
transform: translate(50%, -50%);
|
||||
}
|
||||
.header-top {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 30px;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
}
|
||||
.company-badge {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
.company-logo {
|
||||
width: 60px; height: 60px;
|
||||
background: var(--accent);
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-weight: 800;
|
||||
color: white;
|
||||
font-size: 14px;
|
||||
}
|
||||
.doc-meta {
|
||||
text-align: left;
|
||||
font-size: 12px;
|
||||
opacity: 0.9;
|
||||
}
|
||||
.doc-meta div { margin-bottom: 3px; }
|
||||
.document-title {
|
||||
text-align: center;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
padding: 20px 0;
|
||||
}
|
||||
.document-title .label {
|
||||
display: inline-block;
|
||||
background: rgba(201,169,97,0.2);
|
||||
color: var(--accent);
|
||||
padding: 4px 16px;
|
||||
border-radius: 20px;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 15px;
|
||||
border: 1px solid rgba(201,169,97,0.4);
|
||||
}
|
||||
.document-title h1 {
|
||||
font-size: 28px;
|
||||
font-weight: 800;
|
||||
margin-bottom: 10px;
|
||||
letter-spacing: -0.5px;
|
||||
}
|
||||
.document-title h2 {
|
||||
font-size: 16px;
|
||||
font-weight: 400;
|
||||
opacity: 0.9;
|
||||
}
|
||||
.document-body {
|
||||
padding: 40px;
|
||||
}
|
||||
.intro-text {
|
||||
background: linear-gradient(to left, #f7fafc, #edf2f7);
|
||||
border-right: 4px solid var(--accent);
|
||||
padding: 20px 25px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 35px;
|
||||
font-size: 14px;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
.section {
|
||||
margin-bottom: 40px;
|
||||
page-break-inside: avoid;
|
||||
}
|
||||
.section-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 15px;
|
||||
padding-bottom: 12px;
|
||||
border-bottom: 2px solid var(--border);
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.section-number {
|
||||
width: 50px; height: 50px;
|
||||
background: linear-gradient(135deg, var(--primary), var(--primary-light));
|
||||
color: white;
|
||||
border-radius: 12px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-weight: 800;
|
||||
font-size: 20px;
|
||||
flex-shrink: 0;
|
||||
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
|
||||
}
|
||||
.section-title { flex: 1; }
|
||||
.section-title h3 {
|
||||
font-size: 19px;
|
||||
font-weight: 700;
|
||||
color: var(--primary);
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
.section-title .subtitle {
|
||||
font-size: 12px;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
.section-content {
|
||||
padding-right: 10px;
|
||||
}
|
||||
.section-content p {
|
||||
margin-bottom: 12px;
|
||||
text-align: justify;
|
||||
text-indent: 20px;
|
||||
}
|
||||
.section-content p.no-indent { text-indent: 0; }
|
||||
.highlight-box {
|
||||
background: linear-gradient(to left, #fffaf0, #fef5e7);
|
||||
border: 1px solid #f6e05e;
|
||||
border-right: 4px solid var(--accent);
|
||||
padding: 18px 22px;
|
||||
border-radius: 8px;
|
||||
margin: 20px 0;
|
||||
font-size: 14px;
|
||||
}
|
||||
.highlight-box strong { color: var(--accent-dark); }
|
||||
.info-box {
|
||||
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
|
||||
border: 1px solid #90cdf4;
|
||||
border-right: 4px solid var(--primary-light);
|
||||
padding: 18px 22px;
|
||||
border-radius: 8px;
|
||||
margin: 20px 0;
|
||||
font-size: 14px;
|
||||
}
|
||||
.success-box {
|
||||
background: linear-gradient(to left, #f0fff4, #e6fffa);
|
||||
border: 1px solid #9ae6b4;
|
||||
border-right: 4px solid var(--success);
|
||||
padding: 18px 22px;
|
||||
border-radius: 8px;
|
||||
margin: 20px 0;
|
||||
font-size: 14px;
|
||||
}
|
||||
.warning-box {
|
||||
background: linear-gradient(to left, #fffaf0, #fef5e7);
|
||||
border: 1px solid #fbd38d;
|
||||
border-right: 4px solid var(--warning);
|
||||
padding: 18px 22px;
|
||||
border-radius: 8px;
|
||||
margin: 20px 0;
|
||||
font-size: 14px;
|
||||
}
|
||||
.styled-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin: 20px 0;
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
|
||||
}
|
||||
.styled-table thead {
|
||||
background: linear-gradient(135deg, var(--primary), var(--primary-light));
|
||||
color: white;
|
||||
}
|
||||
.styled-table th {
|
||||
padding: 14px 16px;
|
||||
text-align: right;
|
||||
font-weight: 600;
|
||||
font-size: 13px;
|
||||
}
|
||||
.styled-table td {
|
||||
padding: 13px 16px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
font-size: 13px;
|
||||
}
|
||||
.styled-table tbody tr:last-child td { border-bottom: none; }
|
||||
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
|
||||
.styled-table .highlight-row {
|
||||
background: #fffaf0 !important;
|
||||
font-weight: 600;
|
||||
}
|
||||
.code-block {
|
||||
background: #1a202c;
|
||||
color: #e2e8f0;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
font-family: 'Consolas', 'Monaco', monospace;
|
||||
font-size: 13px;
|
||||
direction: ltr;
|
||||
text-align: left;
|
||||
overflow-x: auto;
|
||||
margin: 20px 0;
|
||||
border-right: 4px solid var(--accent);
|
||||
}
|
||||
.code-block .keyword { color: #569cd6; }
|
||||
.code-block .string { color: #ce9178; }
|
||||
.code-block .comment { color: #6a9955; }
|
||||
.code-block .type { color: #4ec9b0; }
|
||||
.comparison-grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 20px;
|
||||
margin: 25px 0;
|
||||
}
|
||||
.comparison-card {
|
||||
background: #f8fafc;
|
||||
border: 2px solid var(--border);
|
||||
border-radius: 10px;
|
||||
padding: 20px;
|
||||
position: relative;
|
||||
}
|
||||
.comparison-card.recommended {
|
||||
border-color: var(--success);
|
||||
background: linear-gradient(to left, #f0fff4, #e6fffa);
|
||||
}
|
||||
.comparison-card .card-badge {
|
||||
position: absolute;
|
||||
top: -10px;
|
||||
right: 20px;
|
||||
background: var(--success);
|
||||
color: white;
|
||||
padding: 3px 12px;
|
||||
border-radius: 15px;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.comparison-card h4 {
|
||||
font-size: 16px;
|
||||
font-weight: 700;
|
||||
color: var(--primary);
|
||||
margin-bottom: 15px;
|
||||
padding-bottom: 10px;
|
||||
border-bottom: 2px solid var(--border);
|
||||
}
|
||||
.comparison-card.recommended h4 { border-bottom-color: var(--success); }
|
||||
.comparison-card ul { list-style: none; padding: 0; }
|
||||
.comparison-card ul li {
|
||||
padding: 8px 0;
|
||||
border-bottom: 1px solid var(--border);
|
||||
font-size: 13px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
}
|
||||
.comparison-card ul li:last-child { border-bottom: none; }
|
||||
.comparison-card ul li .value { font-weight: 600; color: var(--primary); }
|
||||
.stats-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
gap: 15px;
|
||||
margin: 25px 0;
|
||||
}
|
||||
.stat-card {
|
||||
background: linear-gradient(135deg, var(--primary), var(--primary-light));
|
||||
color: white;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
text-align: center;
|
||||
box-shadow: 0 4px 12px rgba(26,54,93,0.2);
|
||||
}
|
||||
.stat-card .stat-value {
|
||||
font-size: 24px;
|
||||
font-weight: 800;
|
||||
margin-bottom: 5px;
|
||||
color: var(--accent);
|
||||
line-height: 1.3;
|
||||
}
|
||||
.stat-card .stat-label { font-size: 12px; opacity: 0.9; }
|
||||
.conclusion-box {
|
||||
background: linear-gradient(135deg, var(--primary), var(--primary-light));
|
||||
color: white;
|
||||
padding: 30px;
|
||||
border-radius: 12px;
|
||||
margin: 30px 0;
|
||||
text-align: center;
|
||||
box-shadow: 0 8px 20px rgba(26,54,93,0.3);
|
||||
}
|
||||
.conclusion-box h3 {
|
||||
font-size: 22px;
|
||||
font-weight: 800;
|
||||
margin-bottom: 15px;
|
||||
color: var(--accent);
|
||||
}
|
||||
.conclusion-box p {
|
||||
font-size: 14px;
|
||||
line-height: 1.8;
|
||||
text-indent: 0 !important;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
.conclusion-box .highlight {
|
||||
background: rgba(201,169,97,0.2);
|
||||
padding: 15px 20px;
|
||||
border-radius: 8px;
|
||||
margin-top: 20px;
|
||||
border: 1px solid rgba(201,169,97,0.4);
|
||||
}
|
||||
.document-footer {
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
padding: 25px 40px;
|
||||
text-align: center;
|
||||
font-size: 12px;
|
||||
margin-top: 40px;
|
||||
}
|
||||
.document-footer .footer-brand {
|
||||
font-weight: 700;
|
||||
color: var(--accent);
|
||||
margin-bottom: 5px;
|
||||
font-size: 14px;
|
||||
}
|
||||
.footer-divider {
|
||||
width: 60px;
|
||||
height: 2px;
|
||||
background: var(--accent);
|
||||
margin: 10px auto;
|
||||
}
|
||||
.page-info {
|
||||
text-align: center;
|
||||
padding: 15px;
|
||||
font-size: 11px;
|
||||
color: var(--text-muted);
|
||||
border-top: 1px solid var(--border);
|
||||
margin-top: 30px;
|
||||
}
|
||||
@media print {
|
||||
body { background: white; padding: 0; }
|
||||
.document-container { box-shadow: none; border-radius: 0; }
|
||||
.section { page-break-inside: avoid; }
|
||||
}
|
||||
@media (max-width: 768px) {
|
||||
.document-body { padding: 25px 20px; }
|
||||
.document-header { padding: 30px 20px; }
|
||||
.comparison-grid { grid-template-columns: 1fr; }
|
||||
.stats-grid { grid-template-columns: 1fr; }
|
||||
.header-top { flex-direction: column; gap: 15px; }
|
||||
.doc-meta { text-align: center; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="document-container">
|
||||
<header class="document-header">
|
||||
<div class="header-top">
|
||||
<div class="company-badge">
|
||||
<div class="company-logo">RPK</div>
|
||||
</div>
|
||||
<div class="doc-meta">
|
||||
<div>شماره مستند: TECH-1405-003</div>
|
||||
<div>نسخه: ۱.۰</div>
|
||||
<div>تاریخ تهیه: آبان ۱۴۰۵</div>
|
||||
<div>طبقهبندی: داخلی / فنی</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="document-title">
|
||||
<span class="label">TECHNICAL GUIDE</span>
|
||||
<h1>راهنمای جامع ساخت بات بله با C#</h1>
|
||||
<h2>پیادهسازی گامبهگام سرویس نوتیفیکیشن مبتنی بر Bot API</h2>
|
||||
</div>
|
||||
</header>
|
||||
<div class="document-body">
|
||||
<div class="intro-text">
|
||||
<strong>مدیر انتشار و تیم توسعه محترم،</strong><br>
|
||||
این مستند فنی، راهنمای کامل و گامبهگام طراحی و پیادهسازی یک سرویس نوتیفیکیشن مبتنی بر <strong>Bot API استاندارد بله</strong> با استفاده از زبان C# و فریمورک .NET 8 را ارائه میدهد. این روش برای سناریوهایی که کاربران قبلاً با بات تعامل داشتهاند (Chat ID ذخیره شده است) ایدهآل و کاملاً رایگان میباشد.
|
||||
</div>
|
||||
|
||||
<div class="section">
|
||||
<div class="section-header">
|
||||
<div class="section-number">۱</div>
|
||||
<div class="section-title">
|
||||
<h3>خلاصه اجرایی و پیشنیازها</h3>
|
||||
<div class="subtitle">Executive Summary & Prerequisites</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section-content">
|
||||
<p>در این روش، ارتباط با سرورهای بله از طریق آدرس پایه <code>tapi.bale.ai</code> برقرار میشود. کلید موفقیت در این سناریو، داشتن <code>Chat ID</code> منحصربهفرد هر کاربر است که تنها پس از شروع گفتگو توسط کاربر در اختیار بات قرار میگیرد.</p>
|
||||
<div class="stats-grid">
|
||||
<div class="stat-card">
|
||||
<div class="stat-value">رایگان<br>بودن کامل</div>
|
||||
<div class="stat-label">بدون نیاز به شارژ اعتبار یا پنل سفیر</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-value">تعامل<br>دوطرفه</div>
|
||||
<div class="stat-label">امکان دریافت پاسخ و دکمههای شیشهای</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-value">سرعت<br>بالا</div>
|
||||
<div class="stat-label">ارتباط مستقیم HTTP بدون واسطه</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="highlight-box">
|
||||
<strong>نتیجه کلیدی:</strong> برای استفاده از این روش، ابتدا باید مکانیزمی برای جذب کاربر و ذخیره <code>Chat ID</code> او در دیتابیس سازمان پیادهسازی شود. پس از آن، ارسال نوتیفیکیشن در هر زمان ممکن خواهد بود.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="section">
|
||||
<div class="section-header">
|
||||
<div class="section-number">۲</div>
|
||||
<div class="section-title">
|
||||
<h3>گام اول: دریافت توکن و ساختار پروژه</h3>
|
||||
<div class="subtitle">Token Generation & Project Structure</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section-content">
|
||||
<p>برای شروع، باید با مراجعه به بازوی پدر (<code>@botfather</code>) در پیامرسان بله، یک بات جدید ایجاد کرده و توکن احراز هویت (Token) را دریافت نمایید. ساختار پیشنهادی پروژه بر اساس اصول Clean Architecture به شرح زیر است:</p>
|
||||
<div class="code-block">
|
||||
<span class="comment">// ساختار پیشنهادی حل (Solution)</span>
|
||||
BaleNotifier/
|
||||
├── BaleNotifier.sln
|
||||
├── src/
|
||||
│ ├── BaleNotifier.Core/ <span class="comment">// مدلها (Models) و اینترفیسها (Interfaces)</span>
|
||||
│ ├── BaleNotifier.Services/ <span class="comment">// پیادهسازی سرویسهای HTTP و منطق کسبوکار</span>
|
||||
│ └── BaleNotifier.Worker/ <span class="comment">// Background Service برای Polling مداوم</span>
|
||||
└── tests/
|
||||
└── BaleNotifier.Tests/ <span class="comment">// تستهای واحد (Unit Tests)</span>
|
||||
</div>
|
||||
<div class="info-box">
|
||||
<strong>💡 تنظیمات پیکربندی (appsettings.json):</strong>
|
||||
مقدار <code>BotToken</code> دریافتی از باتفادر باید در این فایل یا Azure Key Vault ذخیره شود.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="section">
|
||||
<div class="section-header">
|
||||
<div class="section-number">۳</div>
|
||||
<div class="section-title">
|
||||
<h3>گام دوم: پیادهسازی سرویس Bot API</h3>
|
||||
<div class="subtitle">Bot API Service Implementation</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section-content">
|
||||
<p>سرویس اصلی مسئولیت برقراری ارتباط با متدهای مختلف API بله را بر عهده دارد. پیادهسازی این سرویس با استفاده از <code>HttpClient</code> و تزریق وابستگی (DI) انجام میشود:</p>
|
||||
<div class="code-block">
|
||||
<span class="keyword">public class</span> <span class="type">BaleBotService</span> : <span class="type">IBaleBotService</span>
|
||||
{
|
||||
<span class="keyword">private readonly</span> <span class="type">HttpClient</span> _http;
|
||||
<span class="keyword">private readonly</span> <span class="keyword">string</span> _botToken;
|
||||
|
||||
<span class="keyword">public</span> <span class="type">BaleBotService</span>(<span class="type">HttpClient</span> http, <span class="type">IConfiguration</span> config)
|
||||
{
|
||||
_http = http;
|
||||
_http.BaseAddress = <span class="keyword">new</span> <span class="type">Uri</span>(config[<span class="string">"Bale:BotApiBaseUrl"</span>]!);
|
||||
_botToken = config[<span class="string">"Bale:BotToken"</span>]!;
|
||||
}
|
||||
|
||||
<span class="keyword">public async</span> <span class="type">Task</span><<span class="keyword">bool</span>> <span class="type">SendTextMessageAsync</span>(<span class="keyword">long</span> chatId, <span class="keyword">string</span> text)
|
||||
{
|
||||
<span class="keyword">var</span> payload = <span class="keyword">new</span> { chat_id = chatId, text, parse_mode = <span class="string">"HTML"</span> };
|
||||
<span class="keyword">var</span> response = <span class="keyword">await</span> _http.PostAsJsonAsync($<span class="string">"/bot{_botToken}/sendMessage"</span>, payload);
|
||||
<span class="keyword">var</span> result = <span class="keyword">await</span> response.Content.ReadFromJsonAsync<<span class="type">BaleResponse</span><<span class="type">BaleMessage</span>>>();
|
||||
<span class="keyword">return</span> result?.Ok == <span class="keyword">true</span>;
|
||||
}
|
||||
|
||||
<span class="keyword">public async</span> <span class="type">Task</span><<span class="type">IReadOnlyList</span><<span class="type">BaleUpdate</span>>> <span class="type">GetUpdatesAsync</span>(<span class="keyword">long</span> offset = 0)
|
||||
{
|
||||
<span class="keyword">var</span> url = $<span class="string">"/bot{_botToken}/getUpdates?offset={offset}&timeout=30"</span>;
|
||||
<span class="keyword">var</span> response = <span class="keyword">await</span> _http.GetFromJsonAsync<<span class="type">BaleResponse</span><<span class="type">List</span><<span class="type">BaleUpdate</span>>>>(url);
|
||||
<span class="keyword">return</span> response?.Ok == <span class="keyword">true</span> && response.Result != <span class="keyword">null</span>
|
||||
? response.Result.AsReadOnly()
|
||||
: Array.Empty<<span class="type">BaleUpdate</span>>();
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="section">
|
||||
<div class="section-header">
|
||||
<div class="section-number">۴</div>
|
||||
<div class="section-title">
|
||||
<h3>گام سوم: مدیریت Polling و ذخیره Chat ID</h3>
|
||||
<div class="subtitle">Polling Management & Chat ID Storage</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section-content">
|
||||
<p>برای دریافت پیامهای ارسالی کاربران و استخراج <code>Chat ID</code>، از یک <code>BackgroundService</code> استفاده میکنیم که به صورت مداوم سرور بله را بررسی میکند (Long Polling).</p>
|
||||
<div class="code-block">
|
||||
<span class="keyword">public class</span> <span class="type">BotPollingWorker</span> : <span class="type">BackgroundService</span>
|
||||
{
|
||||
<span class="keyword">private readonly</span> <span class="type">IBaleBotService</span> _bot;
|
||||
<span class="keyword">private long</span> _offset;
|
||||
|
||||
<span class="keyword">protected override async</span> <span class="type">Task</span> <span class="type">ExecuteAsync</span>(<span class="type">CancellationToken</span> ct)
|
||||
{
|
||||
<span class="keyword">while</span> (!ct.IsCancellationRequested)
|
||||
{
|
||||
<span class="keyword">var</span> updates = <span class="keyword">await</span> _bot.GetUpdatesAsync(_offset);
|
||||
<span class="keyword">foreach</span> (<span class="keyword">var</span> update <span class="keyword">in</span> updates)
|
||||
{
|
||||
_offset = update.UpdateId + 1;
|
||||
<span class="keyword">if</span> (update.Message?.Text?.Trim() == <span class="string">"/start"</span>)
|
||||
<span class="keyword">await</span> SubscribeUserAsync(update); <span class="comment">// ذخیره Chat ID در دیتابیس</span>
|
||||
}
|
||||
<span class="keyword">await</span> <span class="type">Task</span>.Delay(1000, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
</div>
|
||||
<div class="success-box">
|
||||
<strong>✓ نکته مهم:</strong> حتماً مقدار <code>offset</code> را پس از هر بار دریافت آپدیتها بهروز کنید تا از پردازش تکراری پیامها جلوگیری شود.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="section">
|
||||
<div class="section-header">
|
||||
<div class="section-number">۵</div>
|
||||
<div class="section-title">
|
||||
<h3>گام چهارم: یکپارچهسازی با چرخه انتشار</h3>
|
||||
<div class="subtitle">Integration with Release Pipeline</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section-content">
|
||||
<p>پس از ذخیره <code>Chat ID</code> ذینفعان در دیتابیس، میتوانید در هر مرحله از چرخه ۷ مرحلهای انتشار، نوتیفیکیشن ارسال کنید. نمونه استفاده در یک سرویس اعلان:</p>
|
||||
<div class="code-block">
|
||||
<span class="keyword">public class</span> <span class="type">ReleaseNotifier</span>
|
||||
{
|
||||
<span class="keyword">private readonly</span> <span class="type">IBaleBotService</span> _bot;
|
||||
<span class="keyword">private readonly</span> <span class="type">IUserRepository</span> _repo;
|
||||
|
||||
<span class="keyword">public async</span> <span class="type">Task</span> <span class="type">OnReleaseDeployed</span>(<span class="keyword">string</span> version)
|
||||
{
|
||||
<span class="keyword">var</span> users = <span class="keyword">await</span> _repo.GetSubscribedUsersAsync();
|
||||
<span class="keyword">var</span> message = $<span class="string">"✅ نسخه <b>{version}</b> با موفقیت در محیط عملیاتی آبی مستقر شد."</span>;
|
||||
|
||||
<span class="keyword">foreach</span> (<span class="keyword">var</span> user <span class="keyword">in</span> users)
|
||||
<span class="keyword">await</span> _bot.SendTextMessageAsync(user.ChatId, message);
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="section">
|
||||
<div class="section-header">
|
||||
<div class="section-number">۶</div>
|
||||
<div class="section-title">
|
||||
<h3>مدیریت خطاها و نکات امنیتی</h3>
|
||||
<div class="subtitle">Error Handling & Security Notes</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section-content">
|
||||
<p>پاسخهای API بله شامل فیلد <code>ok</code> و در صورت خطا، <code>error_code</code> هستند. پایش این کدها برای عیبیابی حیاتی است:</p>
|
||||
<table class="styled-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>کد خطا</th>
|
||||
<th>نام خطا</th>
|
||||
<th>توضیح و راهحل</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>400</td>
|
||||
<td>Bad Request</td>
|
||||
<td>فرمت درخواست اشتباه است (مثلاً Chat ID نامعتبر).</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>401</td>
|
||||
<td>Unauthorized</td>
|
||||
<td>توکن بات اشتباه یا منقضی شده است.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>403</td>
|
||||
<td>Forbidden</td>
|
||||
<td>بات اجازه ارسال پیام به آن کاربر خاص را ندارد (کاربر بات را بلاک کرده است).</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>429</td>
|
||||
<td>Too Many Requests</td>
|
||||
<td>محدودیت نرخ ارسال (Rate Limit). افزایش Delay بین درخواستها.</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="info-box">
|
||||
<strong>🔒 نکته امنیتی:</strong> هرگز <code>BotToken</code> را در مخزن کد (Git Repository) کامیت نکنید. از Azure Key Vault یا GitHub Secrets برای تزریق این مقادیر در زمان اجرا استفاده نمایید.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="section">
|
||||
<div class="section-header">
|
||||
<div class="section-number">۷</div>
|
||||
<div class="section-title">
|
||||
<h3>جمعبندی نهایی و گام بعدی</h3>
|
||||
<div class="subtitle">Final Summary & Next Steps</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section-content">
|
||||
<div class="conclusion-box">
|
||||
<h3>🎯 مسیر پیشنهادی</h3>
|
||||
<p>برای دستیابی به هدف «ارسال نوتیف به کاربران ثبتنام کرده»، پیادهسازی سرویس <strong>Bot API</strong> با الگوی <strong>Polling</strong> و ذخیرهسازی <code>Chat ID</code>، پایدارترین و حرفهایترین راهحل رایگان در اکوسیستم .NET است.</p>
|
||||
<div class="highlight">
|
||||
<strong>💎 ارزش فنی ارائهشده:</strong><br>
|
||||
هزینه صفر + تعامل دوطرفه + مدیریت خودکار خطاها + یکپارچهسازی روان با چرخه انتشار
|
||||
</div>
|
||||
</div>
|
||||
<div class="info-box">
|
||||
<strong>📞 گام بعدی:</strong> در صورت تأیید این معماری، میتوانم ریپازیتوری پایه (Boilerplate) شامل تمامی مدلها، سرویسها و تستهای واحد را آماده و در اختیار تیم توسعه قرار دهم.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="page-info">
|
||||
این مستند فنی محرمانه بوده و صرفاً جهت استفاده داخلی تیم توسعه و مدیریت انتشار شرکت رایان پرداز کاوش تهیه شده است.
|
||||
</div>
|
||||
</div>
|
||||
<footer class="document-footer">
|
||||
<div class="footer-brand">طراحی و تدوین: هادی خزاعی اصل | مدیر انتشار</div>
|
||||
<div class="footer-divider"></div>
|
||||
<div>تاریخ تهیه: آبان ۱۴۰۵ | نسخه: ۱.۰ | طبقهبندی: داخلی / فنی</div>
|
||||
</footer>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user