Skip to content

Commit

Permalink
Merge pull request #2 from AdarshRawat1/Dev_adarsh_Prototype_2
Browse files Browse the repository at this point in the history
[Version 1.1 πŸš€] Post Tags 🏷️ and Telegram BOT πŸ€–
  • Loading branch information
AdarshRawat1 authored Feb 25, 2024
2 parents a890334 + c638867 commit 1777818
Show file tree
Hide file tree
Showing 8 changed files with 1,294 additions and 2 deletions.
10 changes: 9 additions & 1 deletion aid_employ/lib/models/post_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Post {
final String title;
final String? link;
final String? description;
final String? tag;
final String communityName;
final String communityProfilePic;
final List<String> upvotes;
Expand All @@ -20,6 +21,7 @@ class Post {
required this.title,
this.link,
this.description,
this.tag,
required this.communityName,
required this.communityProfilePic,
required this.upvotes,
Expand All @@ -38,6 +40,7 @@ class Post {
String? link,
String? description,
String? communityName,
String? tag,
String? communityProfilePic,
List<String>? upvotes,
List<String>? downvotes,
Expand All @@ -54,6 +57,7 @@ class Post {
link: link ?? this.link,
description: description ?? this.description,
communityName: communityName ?? this.communityName,
tag: tag ?? this.tag,
communityProfilePic: communityProfilePic ?? this.communityProfilePic,
upvotes: upvotes ?? this.upvotes,
downvotes: downvotes ?? this.downvotes,
Expand All @@ -73,6 +77,7 @@ class Post {
'link': link,
'description': description,
'communityName': communityName,
'tag': tag,
'communityProfilePic': communityProfilePic,
'upvotes': upvotes,
'downvotes': downvotes,
Expand All @@ -92,6 +97,7 @@ class Post {
link: map['link'],
description: map['description'],
communityName: map['communityName'] ?? '',
tag: map['tag'] ?? '',
communityProfilePic: map['communityProfilePic'] ?? '',
upvotes: List<String>.from(map['upvotes']),
downvotes: List<String>.from(map['downvotes']),
Expand All @@ -106,7 +112,7 @@ class Post {

@override
String toString() {
return 'Post(id: $id, title: $title, link: $link, description: $description, communityName: $communityName, communityProfilePic: $communityProfilePic, upvotes: $upvotes, downvotes: $downvotes, commentCount: $commentCount, username: $username, uid: $uid, type: $type, createdAt: $createdAt, awards: $awards)';
return 'Post(id: $id, title: $title, link: $link, description: $description, communityName: $communityName, communityProfilePic: $communityProfilePic, tag $tag ,upvotes: $upvotes, downvotes: $downvotes, commentCount: $commentCount, username: $username, uid: $uid, type: $type, createdAt: $createdAt, awards: $awards)';
}

@override
Expand All @@ -119,6 +125,7 @@ class Post {
other.link == link &&
other.description == description &&
other.communityName == communityName &&
other.tag == tag &&
other.communityProfilePic == communityProfilePic &&
listEquals(other.upvotes, upvotes) &&
listEquals(other.downvotes, downvotes) &&
Expand All @@ -143,6 +150,7 @@ class Post {
commentCount.hashCode ^
username.hashCode ^
uid.hashCode ^
tag.hashCode ^
type.hashCode ^
createdAt.hashCode ^
awards.hashCode;
Expand Down
16 changes: 16 additions & 0 deletions aid_employ/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
google_gemini:
dependency: "direct main"
description:
name: google_gemini
sha256: a99e14ba2d497ec8e2b4c83e483204709a4ce3ec6c0a6021b3c4a9b2cd671150
url: "https://pub.dev"
source: hosted
version: "0.1.2"
google_generative_ai:
dependency: "direct main"
description:
name: google_generative_ai
sha256: "2c8cb557c196487c5624da02a0914935e48874112a8201cb7b9379c79cd3bde7"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
google_identity_services_web:
dependency: transitive
description:
Expand Down
4 changes: 3 additions & 1 deletion aid_employ/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ dependencies:
shared_preferences: ^2.0.15
uuid: ^4.3.2
any_link_preview: ^3.0.1

google_gemini: ^0.1.2
google_generative_ai: ^0.2.0

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
Binary file added app-release V1.2.1.apk
Binary file not shown.
12 changes: 12 additions & 0 deletions chatbot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Requirements
- Python 3.x
- `python-telegram-bot` library

## How to Get a Telegram Bot Token

1. Search for the "BotFather" on Telegram.
2. Start a chat and send `/newbot` to create a new bot.
3. Give your bot a name and username.
4. Once the bot is created, BotFather will give you a token. This token is used to authenticate and interact with the Telegram API.

Remember to use the Secrets pane to set the bot and try it out yourself.
130 changes: 130 additions & 0 deletions chatbot/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/env python
# pyright: reportUnusedVariable=false, reportGeneralTypeIssues=false

import logging
from google.cloud.firestore_v1 import collection

from telegram import __version__ as TG_VER

try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]

if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html")

from telegram import ForceReply, Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
from PIL import Image, ImageDraw, ImageFont
import os

import firebase_admin
from firebase_admin import credentials, firestore

my_bot_token = os.environ['YOUR_BOT_TOKEN']

# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)

logger = logging.getLogger(__name__)

# Initialize Firebase
cred = credentials.Certificate("./service.json")
firebase_admin.initialize_app(cred)
db = firestore.client()


# Define a few command handlers. These usually take the two arguments update and
# context.
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:

user = update.effective_user
message = (
f"Hi {user.mention_html()}! πŸ‘‹\n"
"<b><i>I'm Ada</i></b>, your friendly chat bot πŸ€–!\n\n"
"I'm here to help you <b><i>discover social events, community initiatives, and drives</i></b> happening around you.\n"
"Whether you're looking for <b><i>volunteer opportunities, charity events, or community gatherings</i></b>, I've got you covered!\n\n"
"Just share your <b><i>location</i></b> or <b><i>community name</i></b> with me, and I'll fetch the <b><i>latest happenings</i></b> in your area.\n"
"(<i>we are restricted with tasks, so kindly provide only your location in a word or so</i>)\n\n"
"<b><i>Let's explore together!</i></b> 🌍✨")

await update.message.reply_html(message,
reply_markup=ForceReply(selective=True))


async def help_command(update: Update,
context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /help is issued."""
await update.message.reply_text("Help!")


async def location_handler(update: Update,
context: ContextTypes.DEFAULT_TYPE) -> None:
user_message = update.message.text.lower()

collection_ref = db.collection("posts")

city_attribute = "communityName"
city_operation = "=="

# Query documents where the 'communityName' field contains the user's message
documents = collection_ref.where(city_attribute, city_operation,
user_message).limit(5).get()

# Prepare a reply message with the fetched data
reply_text = ""
for document in documents:
title = document.get("title").capitalize()
community = document.get("communityName").capitalize()
description = document.get("description").capitalize()
username = document.get("username").capitalize()

reply_text += (f"<b>πŸ“Œ Title:</b> {title}\n"
f"<b>🌍 Community:</b> {community}\n"
f"<b>ℹ️ Description:</b> {description}\n"
f"<b>πŸ‘€ Username:</b> {username}\n\n")

if not reply_text:
reply_text = f"<b>No data found for the given location.</b>\n\n<i>Probably you've entered something mismatched, kindly try with something else.</i>"
else:
user_message = user_message.capitalize()

reply_text = f"<b>Here's list of some exciting social drives happening in {user_message}! 🌟</b>\n\n{reply_text}"

# Split the reply_text into chunks of maximum message length allowed by Telegram
message_chunks = [
reply_text[i:i + 4096] for i in range(0, len(reply_text), 4096)
]

# Reply to the user with each chunk separately
for chunk in message_chunks:
await update.message.reply_html(chunk)


def main() -> None:
"""Start the bot."""
# Create the Application and pass it your bot's token.
application = Application.builder().token(my_bot_token).build()

# on different commands - answer in Telegram
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("help", help_command))

# on non command i.e message - echo the message on Telegram
application.add_handler(
MessageHandler(filters.TEXT & ~filters.COMMAND, location_handler))

# Run the bot until the user presses Ctrl-C
application.run_polling(allowed_updates=Update.ALL_TYPES)


if __name__ == "__main__":
main()
Loading

0 comments on commit 1777818

Please sign in to comment.